[llvm-objdump] - Remove one overload of reportError. NFCI.
[llvm-complete.git] / lib / IR / Verifier.cpp
blob0350edb2454f5d7497579c0a2826c3ecee27fc45
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 Triple TT;
123 const DataLayout &DL;
124 LLVMContext &Context;
126 /// Track the brokenness of the module while recursively visiting.
127 bool Broken = false;
128 /// Broken debug info can be "recovered" from by stripping the debug info.
129 bool BrokenDebugInfo = false;
130 /// Whether to treat broken debug info as an error.
131 bool TreatBrokenDebugInfoAsError = true;
133 explicit VerifierSupport(raw_ostream *OS, const Module &M)
134 : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
135 Context(M.getContext()) {}
137 private:
138 void Write(const Module *M) {
139 *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
142 void Write(const Value *V) {
143 if (V)
144 Write(*V);
147 void Write(const Value &V) {
148 if (isa<Instruction>(V)) {
149 V.print(*OS, MST);
150 *OS << '\n';
151 } else {
152 V.printAsOperand(*OS, true, MST);
153 *OS << '\n';
157 void Write(const Metadata *MD) {
158 if (!MD)
159 return;
160 MD->print(*OS, MST, &M);
161 *OS << '\n';
164 template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
165 Write(MD.get());
168 void Write(const NamedMDNode *NMD) {
169 if (!NMD)
170 return;
171 NMD->print(*OS, MST);
172 *OS << '\n';
175 void Write(Type *T) {
176 if (!T)
177 return;
178 *OS << ' ' << *T;
181 void Write(const Comdat *C) {
182 if (!C)
183 return;
184 *OS << *C;
187 void Write(const APInt *AI) {
188 if (!AI)
189 return;
190 *OS << *AI << '\n';
193 void Write(const unsigned i) { *OS << i << '\n'; }
195 template <typename T> void Write(ArrayRef<T> Vs) {
196 for (const T &V : Vs)
197 Write(V);
200 template <typename T1, typename... Ts>
201 void WriteTs(const T1 &V1, const Ts &... Vs) {
202 Write(V1);
203 WriteTs(Vs...);
206 template <typename... Ts> void WriteTs() {}
208 public:
209 /// A check failed, so printout out the condition and the message.
211 /// This provides a nice place to put a breakpoint if you want to see why
212 /// something is not correct.
213 void CheckFailed(const Twine &Message) {
214 if (OS)
215 *OS << Message << '\n';
216 Broken = true;
219 /// A check failed (with values to print).
221 /// This calls the Message-only version so that the above is easier to set a
222 /// breakpoint on.
223 template <typename T1, typename... Ts>
224 void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
225 CheckFailed(Message);
226 if (OS)
227 WriteTs(V1, Vs...);
230 /// A debug info check failed.
231 void DebugInfoCheckFailed(const Twine &Message) {
232 if (OS)
233 *OS << Message << '\n';
234 Broken |= TreatBrokenDebugInfoAsError;
235 BrokenDebugInfo = true;
238 /// A debug info check failed (with values to print).
239 template <typename T1, typename... Ts>
240 void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
241 const Ts &... Vs) {
242 DebugInfoCheckFailed(Message);
243 if (OS)
244 WriteTs(V1, Vs...);
248 } // namespace llvm
250 namespace {
252 class Verifier : public InstVisitor<Verifier>, VerifierSupport {
253 friend class InstVisitor<Verifier>;
255 DominatorTree DT;
257 /// When verifying a basic block, keep track of all of the
258 /// instructions we have seen so far.
260 /// This allows us to do efficient dominance checks for the case when an
261 /// instruction has an operand that is an instruction in the same block.
262 SmallPtrSet<Instruction *, 16> InstsInThisBlock;
264 /// Keep track of the metadata nodes that have been checked already.
265 SmallPtrSet<const Metadata *, 32> MDNodes;
267 /// Keep track which DISubprogram is attached to which function.
268 DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments;
270 /// Track all DICompileUnits visited.
271 SmallPtrSet<const Metadata *, 2> CUVisited;
273 /// The result type for a landingpad.
274 Type *LandingPadResultTy;
276 /// Whether we've seen a call to @llvm.localescape in this function
277 /// already.
278 bool SawFrameEscape;
280 /// Whether the current function has a DISubprogram attached to it.
281 bool HasDebugInfo = false;
283 /// Whether source was present on the first DIFile encountered in each CU.
284 DenseMap<const DICompileUnit *, bool> HasSourceDebugInfo;
286 /// Stores the count of how many objects were passed to llvm.localescape for a
287 /// given function and the largest index passed to llvm.localrecover.
288 DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
290 // Maps catchswitches and cleanuppads that unwind to siblings to the
291 // terminators that indicate the unwind, used to detect cycles therein.
292 MapVector<Instruction *, Instruction *> SiblingFuncletInfo;
294 /// Cache of constants visited in search of ConstantExprs.
295 SmallPtrSet<const Constant *, 32> ConstantExprVisited;
297 /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
298 SmallVector<const Function *, 4> DeoptimizeDeclarations;
300 // Verify that this GlobalValue is only used in this module.
301 // This map is used to avoid visiting uses twice. We can arrive at a user
302 // twice, if they have multiple operands. In particular for very large
303 // constant expressions, we can arrive at a particular user many times.
304 SmallPtrSet<const Value *, 32> GlobalValueVisited;
306 // Keeps track of duplicate function argument debug info.
307 SmallVector<const DILocalVariable *, 16> DebugFnArgs;
309 TBAAVerifier TBAAVerifyHelper;
311 void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
313 public:
314 explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
315 const Module &M)
316 : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
317 SawFrameEscape(false), TBAAVerifyHelper(this) {
318 TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
321 bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
323 bool verify(const Function &F) {
324 assert(F.getParent() == &M &&
325 "An instance of this class only works with a specific module!");
327 // First ensure the function is well-enough formed to compute dominance
328 // information, and directly compute a dominance tree. We don't rely on the
329 // pass manager to provide this as it isolates us from a potentially
330 // out-of-date dominator tree and makes it significantly more complex to run
331 // this code outside of a pass manager.
332 // FIXME: It's really gross that we have to cast away constness here.
333 if (!F.empty())
334 DT.recalculate(const_cast<Function &>(F));
336 for (const BasicBlock &BB : F) {
337 if (!BB.empty() && BB.back().isTerminator())
338 continue;
340 if (OS) {
341 *OS << "Basic Block in function '" << F.getName()
342 << "' does not have terminator!\n";
343 BB.printAsOperand(*OS, true, MST);
344 *OS << "\n";
346 return false;
349 Broken = false;
350 // FIXME: We strip const here because the inst visitor strips const.
351 visit(const_cast<Function &>(F));
352 verifySiblingFuncletUnwinds();
353 InstsInThisBlock.clear();
354 DebugFnArgs.clear();
355 LandingPadResultTy = nullptr;
356 SawFrameEscape = false;
357 SiblingFuncletInfo.clear();
359 return !Broken;
362 /// Verify the module that this instance of \c Verifier was initialized with.
363 bool verify() {
364 Broken = false;
366 // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
367 for (const Function &F : M)
368 if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
369 DeoptimizeDeclarations.push_back(&F);
371 // Now that we've visited every function, verify that we never asked to
372 // recover a frame index that wasn't escaped.
373 verifyFrameRecoverIndices();
374 for (const GlobalVariable &GV : M.globals())
375 visitGlobalVariable(GV);
377 for (const GlobalAlias &GA : M.aliases())
378 visitGlobalAlias(GA);
380 for (const NamedMDNode &NMD : M.named_metadata())
381 visitNamedMDNode(NMD);
383 for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
384 visitComdat(SMEC.getValue());
386 visitModuleFlags(M);
387 visitModuleIdents(M);
388 visitModuleCommandLines(M);
390 verifyCompileUnits();
392 verifyDeoptimizeCallingConvs();
393 DISubprogramAttachments.clear();
394 return !Broken;
397 private:
398 // Verification methods...
399 void visitGlobalValue(const GlobalValue &GV);
400 void visitGlobalVariable(const GlobalVariable &GV);
401 void visitGlobalAlias(const GlobalAlias &GA);
402 void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
403 void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
404 const GlobalAlias &A, const Constant &C);
405 void visitNamedMDNode(const NamedMDNode &NMD);
406 void visitMDNode(const MDNode &MD);
407 void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
408 void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
409 void visitComdat(const Comdat &C);
410 void visitModuleIdents(const Module &M);
411 void visitModuleCommandLines(const Module &M);
412 void visitModuleFlags(const Module &M);
413 void visitModuleFlag(const MDNode *Op,
414 DenseMap<const MDString *, const MDNode *> &SeenIDs,
415 SmallVectorImpl<const MDNode *> &Requirements);
416 void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
417 void visitFunction(const Function &F);
418 void visitBasicBlock(BasicBlock &BB);
419 void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
420 void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
421 void visitProfMetadata(Instruction &I, MDNode *MD);
423 template <class Ty> bool isValidMetadataArray(const MDTuple &N);
424 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
425 #include "llvm/IR/Metadata.def"
426 void visitDIScope(const DIScope &N);
427 void visitDIVariable(const DIVariable &N);
428 void visitDILexicalBlockBase(const DILexicalBlockBase &N);
429 void visitDITemplateParameter(const DITemplateParameter &N);
431 void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
433 // InstVisitor overrides...
434 using InstVisitor<Verifier>::visit;
435 void visit(Instruction &I);
437 void visitTruncInst(TruncInst &I);
438 void visitZExtInst(ZExtInst &I);
439 void visitSExtInst(SExtInst &I);
440 void visitFPTruncInst(FPTruncInst &I);
441 void visitFPExtInst(FPExtInst &I);
442 void visitFPToUIInst(FPToUIInst &I);
443 void visitFPToSIInst(FPToSIInst &I);
444 void visitUIToFPInst(UIToFPInst &I);
445 void visitSIToFPInst(SIToFPInst &I);
446 void visitIntToPtrInst(IntToPtrInst &I);
447 void visitPtrToIntInst(PtrToIntInst &I);
448 void visitBitCastInst(BitCastInst &I);
449 void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
450 void visitPHINode(PHINode &PN);
451 void visitCallBase(CallBase &Call);
452 void visitUnaryOperator(UnaryOperator &U);
453 void visitBinaryOperator(BinaryOperator &B);
454 void visitICmpInst(ICmpInst &IC);
455 void visitFCmpInst(FCmpInst &FC);
456 void visitExtractElementInst(ExtractElementInst &EI);
457 void visitInsertElementInst(InsertElementInst &EI);
458 void visitShuffleVectorInst(ShuffleVectorInst &EI);
459 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
460 void visitCallInst(CallInst &CI);
461 void visitInvokeInst(InvokeInst &II);
462 void visitGetElementPtrInst(GetElementPtrInst &GEP);
463 void visitLoadInst(LoadInst &LI);
464 void visitStoreInst(StoreInst &SI);
465 void verifyDominatesUse(Instruction &I, unsigned i);
466 void visitInstruction(Instruction &I);
467 void visitTerminator(Instruction &I);
468 void visitBranchInst(BranchInst &BI);
469 void visitReturnInst(ReturnInst &RI);
470 void visitSwitchInst(SwitchInst &SI);
471 void visitIndirectBrInst(IndirectBrInst &BI);
472 void visitCallBrInst(CallBrInst &CBI);
473 void visitSelectInst(SelectInst &SI);
474 void visitUserOp1(Instruction &I);
475 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
476 void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
477 void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
478 void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
479 void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
480 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
481 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
482 void visitFenceInst(FenceInst &FI);
483 void visitAllocaInst(AllocaInst &AI);
484 void visitExtractValueInst(ExtractValueInst &EVI);
485 void visitInsertValueInst(InsertValueInst &IVI);
486 void visitEHPadPredecessors(Instruction &I);
487 void visitLandingPadInst(LandingPadInst &LPI);
488 void visitResumeInst(ResumeInst &RI);
489 void visitCatchPadInst(CatchPadInst &CPI);
490 void visitCatchReturnInst(CatchReturnInst &CatchReturn);
491 void visitCleanupPadInst(CleanupPadInst &CPI);
492 void visitFuncletPadInst(FuncletPadInst &FPI);
493 void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
494 void visitCleanupReturnInst(CleanupReturnInst &CRI);
496 void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
497 void verifySwiftErrorValue(const Value *SwiftErrorVal);
498 void verifyMustTailCall(CallInst &CI);
499 bool performTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty, int VT,
500 unsigned ArgNo, std::string &Suffix);
501 bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
502 void verifyAttributeTypes(AttributeSet Attrs, bool IsFunction,
503 const Value *V);
504 void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
505 void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
506 const Value *V, bool IsIntrinsic);
507 void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
509 void visitConstantExprsRecursively(const Constant *EntryC);
510 void visitConstantExpr(const ConstantExpr *CE);
511 void verifyStatepoint(const CallBase &Call);
512 void verifyFrameRecoverIndices();
513 void verifySiblingFuncletUnwinds();
515 void verifyFragmentExpression(const DbgVariableIntrinsic &I);
516 template <typename ValueOrMetadata>
517 void verifyFragmentExpression(const DIVariable &V,
518 DIExpression::FragmentInfo Fragment,
519 ValueOrMetadata *Desc);
520 void verifyFnArgs(const DbgVariableIntrinsic &I);
522 /// Module-level debug info verification...
523 void verifyCompileUnits();
525 /// Module-level verification that all @llvm.experimental.deoptimize
526 /// declarations share the same calling convention.
527 void verifyDeoptimizeCallingConvs();
529 /// Verify all-or-nothing property of DIFile source attribute within a CU.
530 void verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F);
533 } // end anonymous namespace
535 /// We know that cond should be true, if not print an error message.
536 #define Assert(C, ...) \
537 do { if (!(C)) { CheckFailed(__VA_ARGS__); return; } } while (false)
539 /// We know that a debug info condition should be true, if not print
540 /// an error message.
541 #define AssertDI(C, ...) \
542 do { if (!(C)) { DebugInfoCheckFailed(__VA_ARGS__); return; } } while (false)
544 void Verifier::visit(Instruction &I) {
545 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
546 Assert(I.getOperand(i) != nullptr, "Operand is null", &I);
547 InstVisitor<Verifier>::visit(I);
550 // Helper to recursively iterate over indirect users. By
551 // returning false, the callback can ask to stop recursing
552 // further.
553 static void forEachUser(const Value *User,
554 SmallPtrSet<const Value *, 32> &Visited,
555 llvm::function_ref<bool(const Value *)> Callback) {
556 if (!Visited.insert(User).second)
557 return;
558 for (const Value *TheNextUser : User->materialized_users())
559 if (Callback(TheNextUser))
560 forEachUser(TheNextUser, Visited, Callback);
563 void Verifier::visitGlobalValue(const GlobalValue &GV) {
564 Assert(!GV.isDeclaration() || GV.hasValidDeclarationLinkage(),
565 "Global is external, but doesn't have external or weak linkage!", &GV);
567 Assert(GV.getAlignment() <= Value::MaximumAlignment,
568 "huge alignment values are unsupported", &GV);
569 Assert(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
570 "Only global variables can have appending linkage!", &GV);
572 if (GV.hasAppendingLinkage()) {
573 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
574 Assert(GVar && GVar->getValueType()->isArrayTy(),
575 "Only global arrays can have appending linkage!", GVar);
578 if (GV.isDeclarationForLinker())
579 Assert(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
581 if (GV.hasDLLImportStorageClass()) {
582 Assert(!GV.isDSOLocal(),
583 "GlobalValue with DLLImport Storage is dso_local!", &GV);
585 Assert((GV.isDeclaration() && GV.hasExternalLinkage()) ||
586 GV.hasAvailableExternallyLinkage(),
587 "Global is marked as dllimport, but not external", &GV);
590 if (GV.hasLocalLinkage())
591 Assert(GV.isDSOLocal(),
592 "GlobalValue with private or internal linkage must be dso_local!",
593 &GV);
595 if (!GV.hasDefaultVisibility() && !GV.hasExternalWeakLinkage())
596 Assert(GV.isDSOLocal(),
597 "GlobalValue with non default visibility must be dso_local!", &GV);
599 forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
600 if (const Instruction *I = dyn_cast<Instruction>(V)) {
601 if (!I->getParent() || !I->getParent()->getParent())
602 CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
604 else if (I->getParent()->getParent()->getParent() != &M)
605 CheckFailed("Global is referenced in a different module!", &GV, &M, I,
606 I->getParent()->getParent(),
607 I->getParent()->getParent()->getParent());
608 return false;
609 } else if (const Function *F = dyn_cast<Function>(V)) {
610 if (F->getParent() != &M)
611 CheckFailed("Global is used by function in a different module", &GV, &M,
612 F, F->getParent());
613 return false;
615 return true;
619 void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
620 if (GV.hasInitializer()) {
621 Assert(GV.getInitializer()->getType() == GV.getValueType(),
622 "Global variable initializer type does not match global "
623 "variable type!",
624 &GV);
625 // If the global has common linkage, it must have a zero initializer and
626 // cannot be constant.
627 if (GV.hasCommonLinkage()) {
628 Assert(GV.getInitializer()->isNullValue(),
629 "'common' global must have a zero initializer!", &GV);
630 Assert(!GV.isConstant(), "'common' global may not be marked constant!",
631 &GV);
632 Assert(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
636 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
637 GV.getName() == "llvm.global_dtors")) {
638 Assert(!GV.hasInitializer() || GV.hasAppendingLinkage(),
639 "invalid linkage for intrinsic global variable", &GV);
640 // Don't worry about emitting an error for it not being an array,
641 // visitGlobalValue will complain on appending non-array.
642 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
643 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
644 PointerType *FuncPtrTy =
645 FunctionType::get(Type::getVoidTy(Context), false)->
646 getPointerTo(DL.getProgramAddressSpace());
647 Assert(STy &&
648 (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
649 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
650 STy->getTypeAtIndex(1) == FuncPtrTy,
651 "wrong type for intrinsic global variable", &GV);
652 Assert(STy->getNumElements() == 3,
653 "the third field of the element type is mandatory, "
654 "specify i8* null to migrate from the obsoleted 2-field form");
655 Type *ETy = STy->getTypeAtIndex(2);
656 Assert(ETy->isPointerTy() &&
657 cast<PointerType>(ETy)->getElementType()->isIntegerTy(8),
658 "wrong type for intrinsic global variable", &GV);
662 if (GV.hasName() && (GV.getName() == "llvm.used" ||
663 GV.getName() == "llvm.compiler.used")) {
664 Assert(!GV.hasInitializer() || GV.hasAppendingLinkage(),
665 "invalid linkage for intrinsic global variable", &GV);
666 Type *GVType = GV.getValueType();
667 if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
668 PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
669 Assert(PTy, "wrong type for intrinsic global variable", &GV);
670 if (GV.hasInitializer()) {
671 const Constant *Init = GV.getInitializer();
672 const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
673 Assert(InitArray, "wrong initalizer for intrinsic global variable",
674 Init);
675 for (Value *Op : InitArray->operands()) {
676 Value *V = Op->stripPointerCasts();
677 Assert(isa<GlobalVariable>(V) || isa<Function>(V) ||
678 isa<GlobalAlias>(V),
679 "invalid llvm.used member", V);
680 Assert(V->hasName(), "members of llvm.used must be named", V);
686 // Visit any debug info attachments.
687 SmallVector<MDNode *, 1> MDs;
688 GV.getMetadata(LLVMContext::MD_dbg, MDs);
689 for (auto *MD : MDs) {
690 if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
691 visitDIGlobalVariableExpression(*GVE);
692 else
693 AssertDI(false, "!dbg attachment of global variable must be a "
694 "DIGlobalVariableExpression");
697 // Scalable vectors cannot be global variables, since we don't know
698 // the runtime size. If the global is a struct or an array containing
699 // scalable vectors, that will be caught by the isValidElementType methods
700 // in StructType or ArrayType instead.
701 if (auto *VTy = dyn_cast<VectorType>(GV.getValueType()))
702 Assert(!VTy->isScalable(), "Globals cannot contain scalable vectors", &GV);
704 if (!GV.hasInitializer()) {
705 visitGlobalValue(GV);
706 return;
709 // Walk any aggregate initializers looking for bitcasts between address spaces
710 visitConstantExprsRecursively(GV.getInitializer());
712 visitGlobalValue(GV);
715 void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
716 SmallPtrSet<const GlobalAlias*, 4> Visited;
717 Visited.insert(&GA);
718 visitAliaseeSubExpr(Visited, GA, C);
721 void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
722 const GlobalAlias &GA, const Constant &C) {
723 if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
724 Assert(!GV->isDeclarationForLinker(), "Alias must point to a definition",
725 &GA);
727 if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
728 Assert(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
730 Assert(!GA2->isInterposable(), "Alias cannot point to an interposable alias",
731 &GA);
732 } else {
733 // Only continue verifying subexpressions of GlobalAliases.
734 // Do not recurse into global initializers.
735 return;
739 if (const auto *CE = dyn_cast<ConstantExpr>(&C))
740 visitConstantExprsRecursively(CE);
742 for (const Use &U : C.operands()) {
743 Value *V = &*U;
744 if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
745 visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
746 else if (const auto *C2 = dyn_cast<Constant>(V))
747 visitAliaseeSubExpr(Visited, GA, *C2);
751 void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
752 Assert(GlobalAlias::isValidLinkage(GA.getLinkage()),
753 "Alias should have private, internal, linkonce, weak, linkonce_odr, "
754 "weak_odr, or external linkage!",
755 &GA);
756 const Constant *Aliasee = GA.getAliasee();
757 Assert(Aliasee, "Aliasee cannot be NULL!", &GA);
758 Assert(GA.getType() == Aliasee->getType(),
759 "Alias and aliasee types should match!", &GA);
761 Assert(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
762 "Aliasee should be either GlobalValue or ConstantExpr", &GA);
764 visitAliaseeSubExpr(GA, *Aliasee);
766 visitGlobalValue(GA);
769 void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
770 // There used to be various other llvm.dbg.* nodes, but we don't support
771 // upgrading them and we want to reserve the namespace for future uses.
772 if (NMD.getName().startswith("llvm.dbg."))
773 AssertDI(NMD.getName() == "llvm.dbg.cu",
774 "unrecognized named metadata node in the llvm.dbg namespace",
775 &NMD);
776 for (const MDNode *MD : NMD.operands()) {
777 if (NMD.getName() == "llvm.dbg.cu")
778 AssertDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
780 if (!MD)
781 continue;
783 visitMDNode(*MD);
787 void Verifier::visitMDNode(const MDNode &MD) {
788 // Only visit each node once. Metadata can be mutually recursive, so this
789 // avoids infinite recursion here, as well as being an optimization.
790 if (!MDNodes.insert(&MD).second)
791 return;
793 switch (MD.getMetadataID()) {
794 default:
795 llvm_unreachable("Invalid MDNode subclass");
796 case Metadata::MDTupleKind:
797 break;
798 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
799 case Metadata::CLASS##Kind: \
800 visit##CLASS(cast<CLASS>(MD)); \
801 break;
802 #include "llvm/IR/Metadata.def"
805 for (const Metadata *Op : MD.operands()) {
806 if (!Op)
807 continue;
808 Assert(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
809 &MD, Op);
810 if (auto *N = dyn_cast<MDNode>(Op)) {
811 visitMDNode(*N);
812 continue;
814 if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
815 visitValueAsMetadata(*V, nullptr);
816 continue;
820 // Check these last, so we diagnose problems in operands first.
821 Assert(!MD.isTemporary(), "Expected no forward declarations!", &MD);
822 Assert(MD.isResolved(), "All nodes should be resolved!", &MD);
825 void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
826 Assert(MD.getValue(), "Expected valid value", &MD);
827 Assert(!MD.getValue()->getType()->isMetadataTy(),
828 "Unexpected metadata round-trip through values", &MD, MD.getValue());
830 auto *L = dyn_cast<LocalAsMetadata>(&MD);
831 if (!L)
832 return;
834 Assert(F, "function-local metadata used outside a function", L);
836 // If this was an instruction, bb, or argument, verify that it is in the
837 // function that we expect.
838 Function *ActualF = nullptr;
839 if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
840 Assert(I->getParent(), "function-local metadata not in basic block", L, I);
841 ActualF = I->getParent()->getParent();
842 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
843 ActualF = BB->getParent();
844 else if (Argument *A = dyn_cast<Argument>(L->getValue()))
845 ActualF = A->getParent();
846 assert(ActualF && "Unimplemented function local metadata case!");
848 Assert(ActualF == F, "function-local metadata used in wrong function", L);
851 void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
852 Metadata *MD = MDV.getMetadata();
853 if (auto *N = dyn_cast<MDNode>(MD)) {
854 visitMDNode(*N);
855 return;
858 // Only visit each node once. Metadata can be mutually recursive, so this
859 // avoids infinite recursion here, as well as being an optimization.
860 if (!MDNodes.insert(MD).second)
861 return;
863 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
864 visitValueAsMetadata(*V, F);
867 static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
868 static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
869 static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
871 void Verifier::visitDILocation(const DILocation &N) {
872 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
873 "location requires a valid scope", &N, N.getRawScope());
874 if (auto *IA = N.getRawInlinedAt())
875 AssertDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
876 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
877 AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
880 void Verifier::visitGenericDINode(const GenericDINode &N) {
881 AssertDI(N.getTag(), "invalid tag", &N);
884 void Verifier::visitDIScope(const DIScope &N) {
885 if (auto *F = N.getRawFile())
886 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
889 void Verifier::visitDISubrange(const DISubrange &N) {
890 AssertDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
891 auto Count = N.getCount();
892 AssertDI(Count, "Count must either be a signed constant or a DIVariable",
893 &N);
894 AssertDI(!Count.is<ConstantInt*>() ||
895 Count.get<ConstantInt*>()->getSExtValue() >= -1,
896 "invalid subrange count", &N);
899 void Verifier::visitDIEnumerator(const DIEnumerator &N) {
900 AssertDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
903 void Verifier::visitDIBasicType(const DIBasicType &N) {
904 AssertDI(N.getTag() == dwarf::DW_TAG_base_type ||
905 N.getTag() == dwarf::DW_TAG_unspecified_type,
906 "invalid tag", &N);
907 AssertDI(!(N.isBigEndian() && N.isLittleEndian()) ,
908 "has conflicting flags", &N);
911 void Verifier::visitDIDerivedType(const DIDerivedType &N) {
912 // Common scope checks.
913 visitDIScope(N);
915 AssertDI(N.getTag() == dwarf::DW_TAG_typedef ||
916 N.getTag() == dwarf::DW_TAG_pointer_type ||
917 N.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
918 N.getTag() == dwarf::DW_TAG_reference_type ||
919 N.getTag() == dwarf::DW_TAG_rvalue_reference_type ||
920 N.getTag() == dwarf::DW_TAG_const_type ||
921 N.getTag() == dwarf::DW_TAG_volatile_type ||
922 N.getTag() == dwarf::DW_TAG_restrict_type ||
923 N.getTag() == dwarf::DW_TAG_atomic_type ||
924 N.getTag() == dwarf::DW_TAG_member ||
925 N.getTag() == dwarf::DW_TAG_inheritance ||
926 N.getTag() == dwarf::DW_TAG_friend,
927 "invalid tag", &N);
928 if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
929 AssertDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
930 N.getRawExtraData());
933 AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
934 AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
935 N.getRawBaseType());
937 if (N.getDWARFAddressSpace()) {
938 AssertDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
939 N.getTag() == dwarf::DW_TAG_reference_type ||
940 N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
941 "DWARF address space only applies to pointer or reference types",
942 &N);
946 /// Detect mutually exclusive flags.
947 static bool hasConflictingReferenceFlags(unsigned Flags) {
948 return ((Flags & DINode::FlagLValueReference) &&
949 (Flags & DINode::FlagRValueReference)) ||
950 ((Flags & DINode::FlagTypePassByValue) &&
951 (Flags & DINode::FlagTypePassByReference));
954 void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
955 auto *Params = dyn_cast<MDTuple>(&RawParams);
956 AssertDI(Params, "invalid template params", &N, &RawParams);
957 for (Metadata *Op : Params->operands()) {
958 AssertDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
959 &N, Params, Op);
963 void Verifier::visitDICompositeType(const DICompositeType &N) {
964 // Common scope checks.
965 visitDIScope(N);
967 AssertDI(N.getTag() == dwarf::DW_TAG_array_type ||
968 N.getTag() == dwarf::DW_TAG_structure_type ||
969 N.getTag() == dwarf::DW_TAG_union_type ||
970 N.getTag() == dwarf::DW_TAG_enumeration_type ||
971 N.getTag() == dwarf::DW_TAG_class_type ||
972 N.getTag() == dwarf::DW_TAG_variant_part,
973 "invalid tag", &N);
975 AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
976 AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
977 N.getRawBaseType());
979 AssertDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
980 "invalid composite elements", &N, N.getRawElements());
981 AssertDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
982 N.getRawVTableHolder());
983 AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
984 "invalid reference flags", &N);
986 if (N.isVector()) {
987 const DINodeArray Elements = N.getElements();
988 AssertDI(Elements.size() == 1 &&
989 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
990 "invalid vector, expected one element of type subrange", &N);
993 if (auto *Params = N.getRawTemplateParams())
994 visitTemplateParams(N, *Params);
996 if (N.getTag() == dwarf::DW_TAG_class_type ||
997 N.getTag() == dwarf::DW_TAG_union_type) {
998 AssertDI(N.getFile() && !N.getFile()->getFilename().empty(),
999 "class/union requires a filename", &N, N.getFile());
1002 if (auto *D = N.getRawDiscriminator()) {
1003 AssertDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
1004 "discriminator can only appear on variant part");
1008 void Verifier::visitDISubroutineType(const DISubroutineType &N) {
1009 AssertDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
1010 if (auto *Types = N.getRawTypeArray()) {
1011 AssertDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
1012 for (Metadata *Ty : N.getTypeArray()->operands()) {
1013 AssertDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
1016 AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
1017 "invalid reference flags", &N);
1020 void Verifier::visitDIFile(const DIFile &N) {
1021 AssertDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
1022 Optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
1023 if (Checksum) {
1024 AssertDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
1025 "invalid checksum kind", &N);
1026 size_t Size;
1027 switch (Checksum->Kind) {
1028 case DIFile::CSK_MD5:
1029 Size = 32;
1030 break;
1031 case DIFile::CSK_SHA1:
1032 Size = 40;
1033 break;
1035 AssertDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
1036 AssertDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
1037 "invalid checksum", &N);
1041 void Verifier::visitDICompileUnit(const DICompileUnit &N) {
1042 AssertDI(N.isDistinct(), "compile units must be distinct", &N);
1043 AssertDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
1045 // Don't bother verifying the compilation directory or producer string
1046 // as those could be empty.
1047 AssertDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
1048 N.getRawFile());
1049 AssertDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
1050 N.getFile());
1052 verifySourceDebugInfo(N, *N.getFile());
1054 AssertDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
1055 "invalid emission kind", &N);
1057 if (auto *Array = N.getRawEnumTypes()) {
1058 AssertDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
1059 for (Metadata *Op : N.getEnumTypes()->operands()) {
1060 auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
1061 AssertDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
1062 "invalid enum type", &N, N.getEnumTypes(), Op);
1065 if (auto *Array = N.getRawRetainedTypes()) {
1066 AssertDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
1067 for (Metadata *Op : N.getRetainedTypes()->operands()) {
1068 AssertDI(Op && (isa<DIType>(Op) ||
1069 (isa<DISubprogram>(Op) &&
1070 !cast<DISubprogram>(Op)->isDefinition())),
1071 "invalid retained type", &N, Op);
1074 if (auto *Array = N.getRawGlobalVariables()) {
1075 AssertDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
1076 for (Metadata *Op : N.getGlobalVariables()->operands()) {
1077 AssertDI(Op && (isa<DIGlobalVariableExpression>(Op)),
1078 "invalid global variable ref", &N, Op);
1081 if (auto *Array = N.getRawImportedEntities()) {
1082 AssertDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
1083 for (Metadata *Op : N.getImportedEntities()->operands()) {
1084 AssertDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
1085 &N, Op);
1088 if (auto *Array = N.getRawMacros()) {
1089 AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1090 for (Metadata *Op : N.getMacros()->operands()) {
1091 AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1094 CUVisited.insert(&N);
1097 void Verifier::visitDISubprogram(const DISubprogram &N) {
1098 AssertDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
1099 AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1100 if (auto *F = N.getRawFile())
1101 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1102 else
1103 AssertDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
1104 if (auto *T = N.getRawType())
1105 AssertDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
1106 AssertDI(isType(N.getRawContainingType()), "invalid containing type", &N,
1107 N.getRawContainingType());
1108 if (auto *Params = N.getRawTemplateParams())
1109 visitTemplateParams(N, *Params);
1110 if (auto *S = N.getRawDeclaration())
1111 AssertDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
1112 "invalid subprogram declaration", &N, S);
1113 if (auto *RawNode = N.getRawRetainedNodes()) {
1114 auto *Node = dyn_cast<MDTuple>(RawNode);
1115 AssertDI(Node, "invalid retained nodes list", &N, RawNode);
1116 for (Metadata *Op : Node->operands()) {
1117 AssertDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op)),
1118 "invalid retained nodes, expected DILocalVariable or DILabel",
1119 &N, Node, Op);
1122 AssertDI(!hasConflictingReferenceFlags(N.getFlags()),
1123 "invalid reference flags", &N);
1125 auto *Unit = N.getRawUnit();
1126 if (N.isDefinition()) {
1127 // Subprogram definitions (not part of the type hierarchy).
1128 AssertDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
1129 AssertDI(Unit, "subprogram definitions must have a compile unit", &N);
1130 AssertDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
1131 if (N.getFile())
1132 verifySourceDebugInfo(*N.getUnit(), *N.getFile());
1133 } else {
1134 // Subprogram declarations (part of the type hierarchy).
1135 AssertDI(!Unit, "subprogram declarations must not have a compile unit", &N);
1138 if (auto *RawThrownTypes = N.getRawThrownTypes()) {
1139 auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
1140 AssertDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
1141 for (Metadata *Op : ThrownTypes->operands())
1142 AssertDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
1143 Op);
1146 if (N.areAllCallsDescribed())
1147 AssertDI(N.isDefinition(),
1148 "DIFlagAllCallsDescribed must be attached to a definition");
1151 void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
1152 AssertDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
1153 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1154 "invalid local scope", &N, N.getRawScope());
1155 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1156 AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1159 void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
1160 visitDILexicalBlockBase(N);
1162 AssertDI(N.getLine() || !N.getColumn(),
1163 "cannot have column info without line info", &N);
1166 void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
1167 visitDILexicalBlockBase(N);
1170 void Verifier::visitDICommonBlock(const DICommonBlock &N) {
1171 AssertDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
1172 if (auto *S = N.getRawScope())
1173 AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1174 if (auto *S = N.getRawDecl())
1175 AssertDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
1178 void Verifier::visitDINamespace(const DINamespace &N) {
1179 AssertDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
1180 if (auto *S = N.getRawScope())
1181 AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1184 void Verifier::visitDIMacro(const DIMacro &N) {
1185 AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
1186 N.getMacinfoType() == dwarf::DW_MACINFO_undef,
1187 "invalid macinfo type", &N);
1188 AssertDI(!N.getName().empty(), "anonymous macro", &N);
1189 if (!N.getValue().empty()) {
1190 assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
1194 void Verifier::visitDIMacroFile(const DIMacroFile &N) {
1195 AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
1196 "invalid macinfo type", &N);
1197 if (auto *F = N.getRawFile())
1198 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1200 if (auto *Array = N.getRawElements()) {
1201 AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1202 for (Metadata *Op : N.getElements()->operands()) {
1203 AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1208 void Verifier::visitDIModule(const DIModule &N) {
1209 AssertDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
1210 AssertDI(!N.getName().empty(), "anonymous module", &N);
1213 void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
1214 AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1217 void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
1218 visitDITemplateParameter(N);
1220 AssertDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
1221 &N);
1224 void Verifier::visitDITemplateValueParameter(
1225 const DITemplateValueParameter &N) {
1226 visitDITemplateParameter(N);
1228 AssertDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
1229 N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
1230 N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
1231 "invalid tag", &N);
1234 void Verifier::visitDIVariable(const DIVariable &N) {
1235 if (auto *S = N.getRawScope())
1236 AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
1237 if (auto *F = N.getRawFile())
1238 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1241 void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
1242 // Checks common to all variables.
1243 visitDIVariable(N);
1245 AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1246 AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1247 AssertDI(N.getType(), "missing global variable type", &N);
1248 if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
1249 AssertDI(isa<DIDerivedType>(Member),
1250 "invalid static data member declaration", &N, Member);
1254 void Verifier::visitDILocalVariable(const DILocalVariable &N) {
1255 // Checks common to all variables.
1256 visitDIVariable(N);
1258 AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1259 AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1260 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1261 "local variable requires a valid scope", &N, N.getRawScope());
1262 if (auto Ty = N.getType())
1263 AssertDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
1266 void Verifier::visitDILabel(const DILabel &N) {
1267 if (auto *S = N.getRawScope())
1268 AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
1269 if (auto *F = N.getRawFile())
1270 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1272 AssertDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
1273 AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1274 "label requires a valid scope", &N, N.getRawScope());
1277 void Verifier::visitDIExpression(const DIExpression &N) {
1278 AssertDI(N.isValid(), "invalid expression", &N);
1281 void Verifier::visitDIGlobalVariableExpression(
1282 const DIGlobalVariableExpression &GVE) {
1283 AssertDI(GVE.getVariable(), "missing variable");
1284 if (auto *Var = GVE.getVariable())
1285 visitDIGlobalVariable(*Var);
1286 if (auto *Expr = GVE.getExpression()) {
1287 visitDIExpression(*Expr);
1288 if (auto Fragment = Expr->getFragmentInfo())
1289 verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
1293 void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
1294 AssertDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
1295 if (auto *T = N.getRawType())
1296 AssertDI(isType(T), "invalid type ref", &N, T);
1297 if (auto *F = N.getRawFile())
1298 AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1301 void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
1302 AssertDI(N.getTag() == dwarf::DW_TAG_imported_module ||
1303 N.getTag() == dwarf::DW_TAG_imported_declaration,
1304 "invalid tag", &N);
1305 if (auto *S = N.getRawScope())
1306 AssertDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
1307 AssertDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
1308 N.getRawEntity());
1311 void Verifier::visitComdat(const Comdat &C) {
1312 // In COFF the Module is invalid if the GlobalValue has private linkage.
1313 // Entities with private linkage don't have entries in the symbol table.
1314 if (TT.isOSBinFormatCOFF())
1315 if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1316 Assert(!GV->hasPrivateLinkage(),
1317 "comdat global value has private linkage", GV);
1320 void Verifier::visitModuleIdents(const Module &M) {
1321 const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
1322 if (!Idents)
1323 return;
1325 // llvm.ident takes a list of metadata entry. Each entry has only one string.
1326 // Scan each llvm.ident entry and make sure that this requirement is met.
1327 for (const MDNode *N : Idents->operands()) {
1328 Assert(N->getNumOperands() == 1,
1329 "incorrect number of operands in llvm.ident metadata", N);
1330 Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
1331 ("invalid value for llvm.ident metadata entry operand"
1332 "(the operand should be a string)"),
1333 N->getOperand(0));
1337 void Verifier::visitModuleCommandLines(const Module &M) {
1338 const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
1339 if (!CommandLines)
1340 return;
1342 // llvm.commandline takes a list of metadata entry. Each entry has only one
1343 // string. Scan each llvm.commandline entry and make sure that this
1344 // requirement is met.
1345 for (const MDNode *N : CommandLines->operands()) {
1346 Assert(N->getNumOperands() == 1,
1347 "incorrect number of operands in llvm.commandline metadata", N);
1348 Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
1349 ("invalid value for llvm.commandline metadata entry operand"
1350 "(the operand should be a string)"),
1351 N->getOperand(0));
1355 void Verifier::visitModuleFlags(const Module &M) {
1356 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
1357 if (!Flags) return;
1359 // Scan each flag, and track the flags and requirements.
1360 DenseMap<const MDString*, const MDNode*> SeenIDs;
1361 SmallVector<const MDNode*, 16> Requirements;
1362 for (const MDNode *MDN : Flags->operands())
1363 visitModuleFlag(MDN, SeenIDs, Requirements);
1365 // Validate that the requirements in the module are valid.
1366 for (const MDNode *Requirement : Requirements) {
1367 const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1368 const Metadata *ReqValue = Requirement->getOperand(1);
1370 const MDNode *Op = SeenIDs.lookup(Flag);
1371 if (!Op) {
1372 CheckFailed("invalid requirement on flag, flag is not present in module",
1373 Flag);
1374 continue;
1377 if (Op->getOperand(2) != ReqValue) {
1378 CheckFailed(("invalid requirement on flag, "
1379 "flag does not have the required value"),
1380 Flag);
1381 continue;
1386 void
1387 Verifier::visitModuleFlag(const MDNode *Op,
1388 DenseMap<const MDString *, const MDNode *> &SeenIDs,
1389 SmallVectorImpl<const MDNode *> &Requirements) {
1390 // Each module flag should have three arguments, the merge behavior (a
1391 // constant int), the flag ID (an MDString), and the value.
1392 Assert(Op->getNumOperands() == 3,
1393 "incorrect number of operands in module flag", Op);
1394 Module::ModFlagBehavior MFB;
1395 if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
1396 Assert(
1397 mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
1398 "invalid behavior operand in module flag (expected constant integer)",
1399 Op->getOperand(0));
1400 Assert(false,
1401 "invalid behavior operand in module flag (unexpected constant)",
1402 Op->getOperand(0));
1404 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1405 Assert(ID, "invalid ID operand in module flag (expected metadata string)",
1406 Op->getOperand(1));
1408 // Sanity check the values for behaviors with additional requirements.
1409 switch (MFB) {
1410 case Module::Error:
1411 case Module::Warning:
1412 case Module::Override:
1413 // These behavior types accept any value.
1414 break;
1416 case Module::Max: {
1417 Assert(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
1418 "invalid value for 'max' module flag (expected constant integer)",
1419 Op->getOperand(2));
1420 break;
1423 case Module::Require: {
1424 // The value should itself be an MDNode with two operands, a flag ID (an
1425 // MDString), and a value.
1426 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
1427 Assert(Value && Value->getNumOperands() == 2,
1428 "invalid value for 'require' module flag (expected metadata pair)",
1429 Op->getOperand(2));
1430 Assert(isa<MDString>(Value->getOperand(0)),
1431 ("invalid value for 'require' module flag "
1432 "(first value operand should be a string)"),
1433 Value->getOperand(0));
1435 // Append it to the list of requirements, to check once all module flags are
1436 // scanned.
1437 Requirements.push_back(Value);
1438 break;
1441 case Module::Append:
1442 case Module::AppendUnique: {
1443 // These behavior types require the operand be an MDNode.
1444 Assert(isa<MDNode>(Op->getOperand(2)),
1445 "invalid value for 'append'-type module flag "
1446 "(expected a metadata node)",
1447 Op->getOperand(2));
1448 break;
1452 // Unless this is a "requires" flag, check the ID is unique.
1453 if (MFB != Module::Require) {
1454 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
1455 Assert(Inserted,
1456 "module flag identifiers must be unique (or of 'require' type)", ID);
1459 if (ID->getString() == "wchar_size") {
1460 ConstantInt *Value
1461 = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1462 Assert(Value, "wchar_size metadata requires constant integer argument");
1465 if (ID->getString() == "Linker Options") {
1466 // If the llvm.linker.options named metadata exists, we assume that the
1467 // bitcode reader has upgraded the module flag. Otherwise the flag might
1468 // have been created by a client directly.
1469 Assert(M.getNamedMetadata("llvm.linker.options"),
1470 "'Linker Options' named metadata no longer supported");
1473 if (ID->getString() == "CG Profile") {
1474 for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
1475 visitModuleFlagCGProfileEntry(MDO);
1479 void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
1480 auto CheckFunction = [&](const MDOperand &FuncMDO) {
1481 if (!FuncMDO)
1482 return;
1483 auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
1484 Assert(F && isa<Function>(F->getValue()), "expected a Function or null",
1485 FuncMDO);
1487 auto Node = dyn_cast_or_null<MDNode>(MDO);
1488 Assert(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
1489 CheckFunction(Node->getOperand(0));
1490 CheckFunction(Node->getOperand(1));
1491 auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
1492 Assert(Count && Count->getType()->isIntegerTy(),
1493 "expected an integer constant", Node->getOperand(2));
1496 /// Return true if this attribute kind only applies to functions.
1497 static bool isFuncOnlyAttr(Attribute::AttrKind Kind) {
1498 switch (Kind) {
1499 case Attribute::NoReturn:
1500 case Attribute::NoSync:
1501 case Attribute::WillReturn:
1502 case Attribute::NoCfCheck:
1503 case Attribute::NoUnwind:
1504 case Attribute::NoInline:
1505 case Attribute::NoFree:
1506 case Attribute::AlwaysInline:
1507 case Attribute::OptimizeForSize:
1508 case Attribute::StackProtect:
1509 case Attribute::StackProtectReq:
1510 case Attribute::StackProtectStrong:
1511 case Attribute::SafeStack:
1512 case Attribute::ShadowCallStack:
1513 case Attribute::NoRedZone:
1514 case Attribute::NoImplicitFloat:
1515 case Attribute::Naked:
1516 case Attribute::InlineHint:
1517 case Attribute::StackAlignment:
1518 case Attribute::UWTable:
1519 case Attribute::NonLazyBind:
1520 case Attribute::ReturnsTwice:
1521 case Attribute::SanitizeAddress:
1522 case Attribute::SanitizeHWAddress:
1523 case Attribute::SanitizeMemTag:
1524 case Attribute::SanitizeThread:
1525 case Attribute::SanitizeMemory:
1526 case Attribute::MinSize:
1527 case Attribute::NoDuplicate:
1528 case Attribute::Builtin:
1529 case Attribute::NoBuiltin:
1530 case Attribute::Cold:
1531 case Attribute::OptForFuzzing:
1532 case Attribute::OptimizeNone:
1533 case Attribute::JumpTable:
1534 case Attribute::Convergent:
1535 case Attribute::ArgMemOnly:
1536 case Attribute::NoRecurse:
1537 case Attribute::InaccessibleMemOnly:
1538 case Attribute::InaccessibleMemOrArgMemOnly:
1539 case Attribute::AllocSize:
1540 case Attribute::SpeculativeLoadHardening:
1541 case Attribute::Speculatable:
1542 case Attribute::StrictFP:
1543 return true;
1544 default:
1545 break;
1547 return false;
1550 /// Return true if this is a function attribute that can also appear on
1551 /// arguments.
1552 static bool isFuncOrArgAttr(Attribute::AttrKind Kind) {
1553 return Kind == Attribute::ReadOnly || Kind == Attribute::WriteOnly ||
1554 Kind == Attribute::ReadNone;
1557 void Verifier::verifyAttributeTypes(AttributeSet Attrs, bool IsFunction,
1558 const Value *V) {
1559 for (Attribute A : Attrs) {
1560 if (A.isStringAttribute())
1561 continue;
1563 if (isFuncOnlyAttr(A.getKindAsEnum())) {
1564 if (!IsFunction) {
1565 CheckFailed("Attribute '" + A.getAsString() +
1566 "' only applies to functions!",
1568 return;
1570 } else if (IsFunction && !isFuncOrArgAttr(A.getKindAsEnum())) {
1571 CheckFailed("Attribute '" + A.getAsString() +
1572 "' does not apply to functions!",
1574 return;
1579 // VerifyParameterAttrs - Check the given attributes for an argument or return
1580 // value of the specified type. The value V is printed in error messages.
1581 void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
1582 const Value *V) {
1583 if (!Attrs.hasAttributes())
1584 return;
1586 verifyAttributeTypes(Attrs, /*IsFunction=*/false, V);
1588 if (Attrs.hasAttribute(Attribute::ImmArg)) {
1589 Assert(Attrs.getNumAttributes() == 1,
1590 "Attribute 'immarg' is incompatible with other attributes", V);
1593 // Check for mutually incompatible attributes. Only inreg is compatible with
1594 // sret.
1595 unsigned AttrCount = 0;
1596 AttrCount += Attrs.hasAttribute(Attribute::ByVal);
1597 AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
1598 AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
1599 Attrs.hasAttribute(Attribute::InReg);
1600 AttrCount += Attrs.hasAttribute(Attribute::Nest);
1601 Assert(AttrCount <= 1, "Attributes 'byval', 'inalloca', 'inreg', 'nest', "
1602 "and 'sret' are incompatible!",
1605 Assert(!(Attrs.hasAttribute(Attribute::InAlloca) &&
1606 Attrs.hasAttribute(Attribute::ReadOnly)),
1607 "Attributes "
1608 "'inalloca and readonly' are incompatible!",
1611 Assert(!(Attrs.hasAttribute(Attribute::StructRet) &&
1612 Attrs.hasAttribute(Attribute::Returned)),
1613 "Attributes "
1614 "'sret and returned' are incompatible!",
1617 Assert(!(Attrs.hasAttribute(Attribute::ZExt) &&
1618 Attrs.hasAttribute(Attribute::SExt)),
1619 "Attributes "
1620 "'zeroext and signext' are incompatible!",
1623 Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1624 Attrs.hasAttribute(Attribute::ReadOnly)),
1625 "Attributes "
1626 "'readnone and readonly' are incompatible!",
1629 Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1630 Attrs.hasAttribute(Attribute::WriteOnly)),
1631 "Attributes "
1632 "'readnone and writeonly' are incompatible!",
1635 Assert(!(Attrs.hasAttribute(Attribute::ReadOnly) &&
1636 Attrs.hasAttribute(Attribute::WriteOnly)),
1637 "Attributes "
1638 "'readonly and writeonly' are incompatible!",
1641 Assert(!(Attrs.hasAttribute(Attribute::NoInline) &&
1642 Attrs.hasAttribute(Attribute::AlwaysInline)),
1643 "Attributes "
1644 "'noinline and alwaysinline' are incompatible!",
1647 if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) {
1648 Assert(Attrs.getByValType() == cast<PointerType>(Ty)->getElementType(),
1649 "Attribute 'byval' type does not match parameter!", V);
1652 AttrBuilder IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
1653 Assert(!AttrBuilder(Attrs).overlaps(IncompatibleAttrs),
1654 "Wrong types for attribute: " +
1655 AttributeSet::get(Context, IncompatibleAttrs).getAsString(),
1658 if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
1659 SmallPtrSet<Type*, 4> Visited;
1660 if (!PTy->getElementType()->isSized(&Visited)) {
1661 Assert(!Attrs.hasAttribute(Attribute::ByVal) &&
1662 !Attrs.hasAttribute(Attribute::InAlloca),
1663 "Attributes 'byval' and 'inalloca' do not support unsized types!",
1666 if (!isa<PointerType>(PTy->getElementType()))
1667 Assert(!Attrs.hasAttribute(Attribute::SwiftError),
1668 "Attribute 'swifterror' only applies to parameters "
1669 "with pointer to pointer type!",
1671 } else {
1672 Assert(!Attrs.hasAttribute(Attribute::ByVal),
1673 "Attribute 'byval' only applies to parameters with pointer type!",
1675 Assert(!Attrs.hasAttribute(Attribute::SwiftError),
1676 "Attribute 'swifterror' only applies to parameters "
1677 "with pointer type!",
1682 // Check parameter attributes against a function type.
1683 // The value V is printed in error messages.
1684 void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
1685 const Value *V, bool IsIntrinsic) {
1686 if (Attrs.isEmpty())
1687 return;
1689 bool SawNest = false;
1690 bool SawReturned = false;
1691 bool SawSRet = false;
1692 bool SawSwiftSelf = false;
1693 bool SawSwiftError = false;
1695 // Verify return value attributes.
1696 AttributeSet RetAttrs = Attrs.getRetAttributes();
1697 Assert((!RetAttrs.hasAttribute(Attribute::ByVal) &&
1698 !RetAttrs.hasAttribute(Attribute::Nest) &&
1699 !RetAttrs.hasAttribute(Attribute::StructRet) &&
1700 !RetAttrs.hasAttribute(Attribute::NoCapture) &&
1701 !RetAttrs.hasAttribute(Attribute::Returned) &&
1702 !RetAttrs.hasAttribute(Attribute::InAlloca) &&
1703 !RetAttrs.hasAttribute(Attribute::SwiftSelf) &&
1704 !RetAttrs.hasAttribute(Attribute::SwiftError)),
1705 "Attributes 'byval', 'inalloca', 'nest', 'sret', 'nocapture', "
1706 "'returned', 'swiftself', and 'swifterror' do not apply to return "
1707 "values!",
1709 Assert((!RetAttrs.hasAttribute(Attribute::ReadOnly) &&
1710 !RetAttrs.hasAttribute(Attribute::WriteOnly) &&
1711 !RetAttrs.hasAttribute(Attribute::ReadNone)),
1712 "Attribute '" + RetAttrs.getAsString() +
1713 "' does not apply to function returns",
1715 verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
1717 // Verify parameter attributes.
1718 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1719 Type *Ty = FT->getParamType(i);
1720 AttributeSet ArgAttrs = Attrs.getParamAttributes(i);
1722 if (!IsIntrinsic) {
1723 Assert(!ArgAttrs.hasAttribute(Attribute::ImmArg),
1724 "immarg attribute only applies to intrinsics",V);
1727 verifyParameterAttrs(ArgAttrs, Ty, V);
1729 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
1730 Assert(!SawNest, "More than one parameter has attribute nest!", V);
1731 SawNest = true;
1734 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
1735 Assert(!SawReturned, "More than one parameter has attribute returned!",
1737 Assert(Ty->canLosslesslyBitCastTo(FT->getReturnType()),
1738 "Incompatible argument and return types for 'returned' attribute",
1740 SawReturned = true;
1743 if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
1744 Assert(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
1745 Assert(i == 0 || i == 1,
1746 "Attribute 'sret' is not on first or second parameter!", V);
1747 SawSRet = true;
1750 if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
1751 Assert(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
1752 SawSwiftSelf = true;
1755 if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
1756 Assert(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!",
1758 SawSwiftError = true;
1761 if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
1762 Assert(i == FT->getNumParams() - 1,
1763 "inalloca isn't on the last parameter!", V);
1767 if (!Attrs.hasAttributes(AttributeList::FunctionIndex))
1768 return;
1770 verifyAttributeTypes(Attrs.getFnAttributes(), /*IsFunction=*/true, V);
1772 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1773 Attrs.hasFnAttribute(Attribute::ReadOnly)),
1774 "Attributes 'readnone and readonly' are incompatible!", V);
1776 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1777 Attrs.hasFnAttribute(Attribute::WriteOnly)),
1778 "Attributes 'readnone and writeonly' are incompatible!", V);
1780 Assert(!(Attrs.hasFnAttribute(Attribute::ReadOnly) &&
1781 Attrs.hasFnAttribute(Attribute::WriteOnly)),
1782 "Attributes 'readonly and writeonly' are incompatible!", V);
1784 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1785 Attrs.hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly)),
1786 "Attributes 'readnone and inaccessiblemem_or_argmemonly' are "
1787 "incompatible!",
1790 Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1791 Attrs.hasFnAttribute(Attribute::InaccessibleMemOnly)),
1792 "Attributes 'readnone and inaccessiblememonly' are incompatible!", V);
1794 Assert(!(Attrs.hasFnAttribute(Attribute::NoInline) &&
1795 Attrs.hasFnAttribute(Attribute::AlwaysInline)),
1796 "Attributes 'noinline and alwaysinline' are incompatible!", V);
1798 if (Attrs.hasFnAttribute(Attribute::OptimizeNone)) {
1799 Assert(Attrs.hasFnAttribute(Attribute::NoInline),
1800 "Attribute 'optnone' requires 'noinline'!", V);
1802 Assert(!Attrs.hasFnAttribute(Attribute::OptimizeForSize),
1803 "Attributes 'optsize and optnone' are incompatible!", V);
1805 Assert(!Attrs.hasFnAttribute(Attribute::MinSize),
1806 "Attributes 'minsize and optnone' are incompatible!", V);
1809 if (Attrs.hasFnAttribute(Attribute::JumpTable)) {
1810 const GlobalValue *GV = cast<GlobalValue>(V);
1811 Assert(GV->hasGlobalUnnamedAddr(),
1812 "Attribute 'jumptable' requires 'unnamed_addr'", V);
1815 if (Attrs.hasFnAttribute(Attribute::AllocSize)) {
1816 std::pair<unsigned, Optional<unsigned>> Args =
1817 Attrs.getAllocSizeArgs(AttributeList::FunctionIndex);
1819 auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
1820 if (ParamNo >= FT->getNumParams()) {
1821 CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
1822 return false;
1825 if (!FT->getParamType(ParamNo)->isIntegerTy()) {
1826 CheckFailed("'allocsize' " + Name +
1827 " argument must refer to an integer parameter",
1829 return false;
1832 return true;
1835 if (!CheckParam("element size", Args.first))
1836 return;
1838 if (Args.second && !CheckParam("number of elements", *Args.second))
1839 return;
1843 void Verifier::verifyFunctionMetadata(
1844 ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
1845 for (const auto &Pair : MDs) {
1846 if (Pair.first == LLVMContext::MD_prof) {
1847 MDNode *MD = Pair.second;
1848 Assert(MD->getNumOperands() >= 2,
1849 "!prof annotations should have no less than 2 operands", MD);
1851 // Check first operand.
1852 Assert(MD->getOperand(0) != nullptr, "first operand should not be null",
1853 MD);
1854 Assert(isa<MDString>(MD->getOperand(0)),
1855 "expected string with name of the !prof annotation", MD);
1856 MDString *MDS = cast<MDString>(MD->getOperand(0));
1857 StringRef ProfName = MDS->getString();
1858 Assert(ProfName.equals("function_entry_count") ||
1859 ProfName.equals("synthetic_function_entry_count"),
1860 "first operand should be 'function_entry_count'"
1861 " or 'synthetic_function_entry_count'",
1862 MD);
1864 // Check second operand.
1865 Assert(MD->getOperand(1) != nullptr, "second operand should not be null",
1866 MD);
1867 Assert(isa<ConstantAsMetadata>(MD->getOperand(1)),
1868 "expected integer argument to function_entry_count", MD);
1873 void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
1874 if (!ConstantExprVisited.insert(EntryC).second)
1875 return;
1877 SmallVector<const Constant *, 16> Stack;
1878 Stack.push_back(EntryC);
1880 while (!Stack.empty()) {
1881 const Constant *C = Stack.pop_back_val();
1883 // Check this constant expression.
1884 if (const auto *CE = dyn_cast<ConstantExpr>(C))
1885 visitConstantExpr(CE);
1887 if (const auto *GV = dyn_cast<GlobalValue>(C)) {
1888 // Global Values get visited separately, but we do need to make sure
1889 // that the global value is in the correct module
1890 Assert(GV->getParent() == &M, "Referencing global in another module!",
1891 EntryC, &M, GV, GV->getParent());
1892 continue;
1895 // Visit all sub-expressions.
1896 for (const Use &U : C->operands()) {
1897 const auto *OpC = dyn_cast<Constant>(U);
1898 if (!OpC)
1899 continue;
1900 if (!ConstantExprVisited.insert(OpC).second)
1901 continue;
1902 Stack.push_back(OpC);
1907 void Verifier::visitConstantExpr(const ConstantExpr *CE) {
1908 if (CE->getOpcode() == Instruction::BitCast)
1909 Assert(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
1910 CE->getType()),
1911 "Invalid bitcast", CE);
1913 if (CE->getOpcode() == Instruction::IntToPtr ||
1914 CE->getOpcode() == Instruction::PtrToInt) {
1915 auto *PtrTy = CE->getOpcode() == Instruction::IntToPtr
1916 ? CE->getType()
1917 : CE->getOperand(0)->getType();
1918 StringRef Msg = CE->getOpcode() == Instruction::IntToPtr
1919 ? "inttoptr not supported for non-integral pointers"
1920 : "ptrtoint not supported for non-integral pointers";
1921 Assert(
1922 !DL.isNonIntegralPointerType(cast<PointerType>(PtrTy->getScalarType())),
1923 Msg);
1927 bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
1928 // There shouldn't be more attribute sets than there are parameters plus the
1929 // function and return value.
1930 return Attrs.getNumAttrSets() <= Params + 2;
1933 /// Verify that statepoint intrinsic is well formed.
1934 void Verifier::verifyStatepoint(const CallBase &Call) {
1935 assert(Call.getCalledFunction() &&
1936 Call.getCalledFunction()->getIntrinsicID() ==
1937 Intrinsic::experimental_gc_statepoint);
1939 Assert(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
1940 !Call.onlyAccessesArgMemory(),
1941 "gc.statepoint must read and write all memory to preserve "
1942 "reordering restrictions required by safepoint semantics",
1943 Call);
1945 const int64_t NumPatchBytes =
1946 cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
1947 assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
1948 Assert(NumPatchBytes >= 0,
1949 "gc.statepoint number of patchable bytes must be "
1950 "positive",
1951 Call);
1953 const Value *Target = Call.getArgOperand(2);
1954 auto *PT = dyn_cast<PointerType>(Target->getType());
1955 Assert(PT && PT->getElementType()->isFunctionTy(),
1956 "gc.statepoint callee must be of function pointer type", Call, Target);
1957 FunctionType *TargetFuncType = cast<FunctionType>(PT->getElementType());
1959 const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
1960 Assert(NumCallArgs >= 0,
1961 "gc.statepoint number of arguments to underlying call "
1962 "must be positive",
1963 Call);
1964 const int NumParams = (int)TargetFuncType->getNumParams();
1965 if (TargetFuncType->isVarArg()) {
1966 Assert(NumCallArgs >= NumParams,
1967 "gc.statepoint mismatch in number of vararg call args", Call);
1969 // TODO: Remove this limitation
1970 Assert(TargetFuncType->getReturnType()->isVoidTy(),
1971 "gc.statepoint doesn't support wrapping non-void "
1972 "vararg functions yet",
1973 Call);
1974 } else
1975 Assert(NumCallArgs == NumParams,
1976 "gc.statepoint mismatch in number of call args", Call);
1978 const uint64_t Flags
1979 = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
1980 Assert((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
1981 "unknown flag used in gc.statepoint flags argument", Call);
1983 // Verify that the types of the call parameter arguments match
1984 // the type of the wrapped callee.
1985 AttributeList Attrs = Call.getAttributes();
1986 for (int i = 0; i < NumParams; i++) {
1987 Type *ParamType = TargetFuncType->getParamType(i);
1988 Type *ArgType = Call.getArgOperand(5 + i)->getType();
1989 Assert(ArgType == ParamType,
1990 "gc.statepoint call argument does not match wrapped "
1991 "function type",
1992 Call);
1994 if (TargetFuncType->isVarArg()) {
1995 AttributeSet ArgAttrs = Attrs.getParamAttributes(5 + i);
1996 Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
1997 "Attribute 'sret' cannot be used for vararg call arguments!",
1998 Call);
2002 const int EndCallArgsInx = 4 + NumCallArgs;
2004 const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
2005 Assert(isa<ConstantInt>(NumTransitionArgsV),
2006 "gc.statepoint number of transition arguments "
2007 "must be constant integer",
2008 Call);
2009 const int NumTransitionArgs =
2010 cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
2011 Assert(NumTransitionArgs >= 0,
2012 "gc.statepoint number of transition arguments must be positive", Call);
2013 const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
2015 const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
2016 Assert(isa<ConstantInt>(NumDeoptArgsV),
2017 "gc.statepoint number of deoptimization arguments "
2018 "must be constant integer",
2019 Call);
2020 const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
2021 Assert(NumDeoptArgs >= 0,
2022 "gc.statepoint number of deoptimization arguments "
2023 "must be positive",
2024 Call);
2026 const int ExpectedNumArgs =
2027 7 + NumCallArgs + NumTransitionArgs + NumDeoptArgs;
2028 Assert(ExpectedNumArgs <= (int)Call.arg_size(),
2029 "gc.statepoint too few arguments according to length fields", Call);
2031 // Check that the only uses of this gc.statepoint are gc.result or
2032 // gc.relocate calls which are tied to this statepoint and thus part
2033 // of the same statepoint sequence
2034 for (const User *U : Call.users()) {
2035 const CallInst *UserCall = dyn_cast<const CallInst>(U);
2036 Assert(UserCall, "illegal use of statepoint token", Call, U);
2037 if (!UserCall)
2038 continue;
2039 Assert(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
2040 "gc.result or gc.relocate are the only value uses "
2041 "of a gc.statepoint",
2042 Call, U);
2043 if (isa<GCResultInst>(UserCall)) {
2044 Assert(UserCall->getArgOperand(0) == &Call,
2045 "gc.result connected to wrong gc.statepoint", Call, UserCall);
2046 } else if (isa<GCRelocateInst>(Call)) {
2047 Assert(UserCall->getArgOperand(0) == &Call,
2048 "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
2052 // Note: It is legal for a single derived pointer to be listed multiple
2053 // times. It's non-optimal, but it is legal. It can also happen after
2054 // insertion if we strip a bitcast away.
2055 // Note: It is really tempting to check that each base is relocated and
2056 // that a derived pointer is never reused as a base pointer. This turns
2057 // out to be problematic since optimizations run after safepoint insertion
2058 // can recognize equality properties that the insertion logic doesn't know
2059 // about. See example statepoint.ll in the verifier subdirectory
2062 void Verifier::verifyFrameRecoverIndices() {
2063 for (auto &Counts : FrameEscapeInfo) {
2064 Function *F = Counts.first;
2065 unsigned EscapedObjectCount = Counts.second.first;
2066 unsigned MaxRecoveredIndex = Counts.second.second;
2067 Assert(MaxRecoveredIndex <= EscapedObjectCount,
2068 "all indices passed to llvm.localrecover must be less than the "
2069 "number of arguments passed to llvm.localescape in the parent "
2070 "function",
2075 static Instruction *getSuccPad(Instruction *Terminator) {
2076 BasicBlock *UnwindDest;
2077 if (auto *II = dyn_cast<InvokeInst>(Terminator))
2078 UnwindDest = II->getUnwindDest();
2079 else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
2080 UnwindDest = CSI->getUnwindDest();
2081 else
2082 UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
2083 return UnwindDest->getFirstNonPHI();
2086 void Verifier::verifySiblingFuncletUnwinds() {
2087 SmallPtrSet<Instruction *, 8> Visited;
2088 SmallPtrSet<Instruction *, 8> Active;
2089 for (const auto &Pair : SiblingFuncletInfo) {
2090 Instruction *PredPad = Pair.first;
2091 if (Visited.count(PredPad))
2092 continue;
2093 Active.insert(PredPad);
2094 Instruction *Terminator = Pair.second;
2095 do {
2096 Instruction *SuccPad = getSuccPad(Terminator);
2097 if (Active.count(SuccPad)) {
2098 // Found a cycle; report error
2099 Instruction *CyclePad = SuccPad;
2100 SmallVector<Instruction *, 8> CycleNodes;
2101 do {
2102 CycleNodes.push_back(CyclePad);
2103 Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
2104 if (CycleTerminator != CyclePad)
2105 CycleNodes.push_back(CycleTerminator);
2106 CyclePad = getSuccPad(CycleTerminator);
2107 } while (CyclePad != SuccPad);
2108 Assert(false, "EH pads can't handle each other's exceptions",
2109 ArrayRef<Instruction *>(CycleNodes));
2111 // Don't re-walk a node we've already checked
2112 if (!Visited.insert(SuccPad).second)
2113 break;
2114 // Walk to this successor if it has a map entry.
2115 PredPad = SuccPad;
2116 auto TermI = SiblingFuncletInfo.find(PredPad);
2117 if (TermI == SiblingFuncletInfo.end())
2118 break;
2119 Terminator = TermI->second;
2120 Active.insert(PredPad);
2121 } while (true);
2122 // Each node only has one successor, so we've walked all the active
2123 // nodes' successors.
2124 Active.clear();
2128 // visitFunction - Verify that a function is ok.
2130 void Verifier::visitFunction(const Function &F) {
2131 visitGlobalValue(F);
2133 // Check function arguments.
2134 FunctionType *FT = F.getFunctionType();
2135 unsigned NumArgs = F.arg_size();
2137 Assert(&Context == &F.getContext(),
2138 "Function context does not match Module context!", &F);
2140 Assert(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
2141 Assert(FT->getNumParams() == NumArgs,
2142 "# formal arguments must match # of arguments for function type!", &F,
2143 FT);
2144 Assert(F.getReturnType()->isFirstClassType() ||
2145 F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
2146 "Functions cannot return aggregate values!", &F);
2148 Assert(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
2149 "Invalid struct return type!", &F);
2151 AttributeList Attrs = F.getAttributes();
2153 Assert(verifyAttributeCount(Attrs, FT->getNumParams()),
2154 "Attribute after last parameter!", &F);
2156 bool isLLVMdotName = F.getName().size() >= 5 &&
2157 F.getName().substr(0, 5) == "llvm.";
2159 // Check function attributes.
2160 verifyFunctionAttrs(FT, Attrs, &F, isLLVMdotName);
2162 // On function declarations/definitions, we do not support the builtin
2163 // attribute. We do not check this in VerifyFunctionAttrs since that is
2164 // checking for Attributes that can/can not ever be on functions.
2165 Assert(!Attrs.hasFnAttribute(Attribute::Builtin),
2166 "Attribute 'builtin' can only be applied to a callsite.", &F);
2168 // Check that this function meets the restrictions on this calling convention.
2169 // Sometimes varargs is used for perfectly forwarding thunks, so some of these
2170 // restrictions can be lifted.
2171 switch (F.getCallingConv()) {
2172 default:
2173 case CallingConv::C:
2174 break;
2175 case CallingConv::AMDGPU_KERNEL:
2176 case CallingConv::SPIR_KERNEL:
2177 Assert(F.getReturnType()->isVoidTy(),
2178 "Calling convention requires void return type", &F);
2179 LLVM_FALLTHROUGH;
2180 case CallingConv::AMDGPU_VS:
2181 case CallingConv::AMDGPU_HS:
2182 case CallingConv::AMDGPU_GS:
2183 case CallingConv::AMDGPU_PS:
2184 case CallingConv::AMDGPU_CS:
2185 Assert(!F.hasStructRetAttr(),
2186 "Calling convention does not allow sret", &F);
2187 LLVM_FALLTHROUGH;
2188 case CallingConv::Fast:
2189 case CallingConv::Cold:
2190 case CallingConv::Intel_OCL_BI:
2191 case CallingConv::PTX_Kernel:
2192 case CallingConv::PTX_Device:
2193 Assert(!F.isVarArg(), "Calling convention does not support varargs or "
2194 "perfect forwarding!",
2195 &F);
2196 break;
2199 // Check that the argument values match the function type for this function...
2200 unsigned i = 0;
2201 for (const Argument &Arg : F.args()) {
2202 Assert(Arg.getType() == FT->getParamType(i),
2203 "Argument value does not match function argument type!", &Arg,
2204 FT->getParamType(i));
2205 Assert(Arg.getType()->isFirstClassType(),
2206 "Function arguments must have first-class types!", &Arg);
2207 if (!isLLVMdotName) {
2208 Assert(!Arg.getType()->isMetadataTy(),
2209 "Function takes metadata but isn't an intrinsic", &Arg, &F);
2210 Assert(!Arg.getType()->isTokenTy(),
2211 "Function takes token but isn't an intrinsic", &Arg, &F);
2214 // Check that swifterror argument is only used by loads and stores.
2215 if (Attrs.hasParamAttribute(i, Attribute::SwiftError)) {
2216 verifySwiftErrorValue(&Arg);
2218 ++i;
2221 if (!isLLVMdotName)
2222 Assert(!F.getReturnType()->isTokenTy(),
2223 "Functions returns a token but isn't an intrinsic", &F);
2225 // Get the function metadata attachments.
2226 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2227 F.getAllMetadata(MDs);
2228 assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
2229 verifyFunctionMetadata(MDs);
2231 // Check validity of the personality function
2232 if (F.hasPersonalityFn()) {
2233 auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
2234 if (Per)
2235 Assert(Per->getParent() == F.getParent(),
2236 "Referencing personality function in another module!",
2237 &F, F.getParent(), Per, Per->getParent());
2240 if (F.isMaterializable()) {
2241 // Function has a body somewhere we can't see.
2242 Assert(MDs.empty(), "unmaterialized function cannot have metadata", &F,
2243 MDs.empty() ? nullptr : MDs.front().second);
2244 } else if (F.isDeclaration()) {
2245 for (const auto &I : MDs) {
2246 // This is used for call site debug information.
2247 AssertDI(I.first != LLVMContext::MD_dbg ||
2248 !cast<DISubprogram>(I.second)->isDistinct(),
2249 "function declaration may only have a unique !dbg attachment",
2250 &F);
2251 Assert(I.first != LLVMContext::MD_prof,
2252 "function declaration may not have a !prof attachment", &F);
2254 // Verify the metadata itself.
2255 visitMDNode(*I.second);
2257 Assert(!F.hasPersonalityFn(),
2258 "Function declaration shouldn't have a personality routine", &F);
2259 } else {
2260 // Verify that this function (which has a body) is not named "llvm.*". It
2261 // is not legal to define intrinsics.
2262 Assert(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
2264 // Check the entry node
2265 const BasicBlock *Entry = &F.getEntryBlock();
2266 Assert(pred_empty(Entry),
2267 "Entry block to function must not have predecessors!", Entry);
2269 // The address of the entry block cannot be taken, unless it is dead.
2270 if (Entry->hasAddressTaken()) {
2271 Assert(!BlockAddress::lookup(Entry)->isConstantUsed(),
2272 "blockaddress may not be used with the entry block!", Entry);
2275 unsigned NumDebugAttachments = 0, NumProfAttachments = 0;
2276 // Visit metadata attachments.
2277 for (const auto &I : MDs) {
2278 // Verify that the attachment is legal.
2279 switch (I.first) {
2280 default:
2281 break;
2282 case LLVMContext::MD_dbg: {
2283 ++NumDebugAttachments;
2284 AssertDI(NumDebugAttachments == 1,
2285 "function must have a single !dbg attachment", &F, I.second);
2286 AssertDI(isa<DISubprogram>(I.second),
2287 "function !dbg attachment must be a subprogram", &F, I.second);
2288 auto *SP = cast<DISubprogram>(I.second);
2289 const Function *&AttachedTo = DISubprogramAttachments[SP];
2290 AssertDI(!AttachedTo || AttachedTo == &F,
2291 "DISubprogram attached to more than one function", SP, &F);
2292 AttachedTo = &F;
2293 break;
2295 case LLVMContext::MD_prof:
2296 ++NumProfAttachments;
2297 Assert(NumProfAttachments == 1,
2298 "function must have a single !prof attachment", &F, I.second);
2299 break;
2302 // Verify the metadata itself.
2303 visitMDNode(*I.second);
2307 // If this function is actually an intrinsic, verify that it is only used in
2308 // direct call/invokes, never having its "address taken".
2309 // Only do this if the module is materialized, otherwise we don't have all the
2310 // uses.
2311 if (F.getIntrinsicID() && F.getParent()->isMaterialized()) {
2312 const User *U;
2313 if (F.hasAddressTaken(&U))
2314 Assert(false, "Invalid user of intrinsic instruction!", U);
2317 auto *N = F.getSubprogram();
2318 HasDebugInfo = (N != nullptr);
2319 if (!HasDebugInfo)
2320 return;
2322 // Check that all !dbg attachments lead to back to N (or, at least, another
2323 // subprogram that describes the same function).
2325 // FIXME: Check this incrementally while visiting !dbg attachments.
2326 // FIXME: Only check when N is the canonical subprogram for F.
2327 SmallPtrSet<const MDNode *, 32> Seen;
2328 auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
2329 // Be careful about using DILocation here since we might be dealing with
2330 // broken code (this is the Verifier after all).
2331 const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
2332 if (!DL)
2333 return;
2334 if (!Seen.insert(DL).second)
2335 return;
2337 Metadata *Parent = DL->getRawScope();
2338 AssertDI(Parent && isa<DILocalScope>(Parent),
2339 "DILocation's scope must be a DILocalScope", N, &F, &I, DL,
2340 Parent);
2341 DILocalScope *Scope = DL->getInlinedAtScope();
2342 if (Scope && !Seen.insert(Scope).second)
2343 return;
2345 DISubprogram *SP = Scope ? Scope->getSubprogram() : nullptr;
2347 // Scope and SP could be the same MDNode and we don't want to skip
2348 // validation in that case
2349 if (SP && ((Scope != SP) && !Seen.insert(SP).second))
2350 return;
2352 // FIXME: Once N is canonical, check "SP == &N".
2353 AssertDI(SP->describes(&F),
2354 "!dbg attachment points at wrong subprogram for function", N, &F,
2355 &I, DL, Scope, SP);
2357 for (auto &BB : F)
2358 for (auto &I : BB) {
2359 VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
2360 // The llvm.loop annotations also contain two DILocations.
2361 if (auto MD = I.getMetadata(LLVMContext::MD_loop))
2362 for (unsigned i = 1; i < MD->getNumOperands(); ++i)
2363 VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
2364 if (BrokenDebugInfo)
2365 return;
2369 // verifyBasicBlock - Verify that a basic block is well formed...
2371 void Verifier::visitBasicBlock(BasicBlock &BB) {
2372 InstsInThisBlock.clear();
2374 // Ensure that basic blocks have terminators!
2375 Assert(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
2377 // Check constraints that this basic block imposes on all of the PHI nodes in
2378 // it.
2379 if (isa<PHINode>(BB.front())) {
2380 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
2381 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
2382 llvm::sort(Preds);
2383 for (const PHINode &PN : BB.phis()) {
2384 // Ensure that PHI nodes have at least one entry!
2385 Assert(PN.getNumIncomingValues() != 0,
2386 "PHI nodes must have at least one entry. If the block is dead, "
2387 "the PHI should be removed!",
2388 &PN);
2389 Assert(PN.getNumIncomingValues() == Preds.size(),
2390 "PHINode should have one entry for each predecessor of its "
2391 "parent basic block!",
2392 &PN);
2394 // Get and sort all incoming values in the PHI node...
2395 Values.clear();
2396 Values.reserve(PN.getNumIncomingValues());
2397 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2398 Values.push_back(
2399 std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
2400 llvm::sort(Values);
2402 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2403 // Check to make sure that if there is more than one entry for a
2404 // particular basic block in this PHI node, that the incoming values are
2405 // all identical.
2407 Assert(i == 0 || Values[i].first != Values[i - 1].first ||
2408 Values[i].second == Values[i - 1].second,
2409 "PHI node has multiple entries for the same basic block with "
2410 "different incoming values!",
2411 &PN, Values[i].first, Values[i].second, Values[i - 1].second);
2413 // Check to make sure that the predecessors and PHI node entries are
2414 // matched up.
2415 Assert(Values[i].first == Preds[i],
2416 "PHI node entries do not match predecessors!", &PN,
2417 Values[i].first, Preds[i]);
2422 // Check that all instructions have their parent pointers set up correctly.
2423 for (auto &I : BB)
2425 Assert(I.getParent() == &BB, "Instruction has bogus parent pointer!");
2429 void Verifier::visitTerminator(Instruction &I) {
2430 // Ensure that terminators only exist at the end of the basic block.
2431 Assert(&I == I.getParent()->getTerminator(),
2432 "Terminator found in the middle of a basic block!", I.getParent());
2433 visitInstruction(I);
2436 void Verifier::visitBranchInst(BranchInst &BI) {
2437 if (BI.isConditional()) {
2438 Assert(BI.getCondition()->getType()->isIntegerTy(1),
2439 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
2441 visitTerminator(BI);
2444 void Verifier::visitReturnInst(ReturnInst &RI) {
2445 Function *F = RI.getParent()->getParent();
2446 unsigned N = RI.getNumOperands();
2447 if (F->getReturnType()->isVoidTy())
2448 Assert(N == 0,
2449 "Found return instr that returns non-void in Function of void "
2450 "return type!",
2451 &RI, F->getReturnType());
2452 else
2453 Assert(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
2454 "Function return type does not match operand "
2455 "type of return inst!",
2456 &RI, F->getReturnType());
2458 // Check to make sure that the return value has necessary properties for
2459 // terminators...
2460 visitTerminator(RI);
2463 void Verifier::visitSwitchInst(SwitchInst &SI) {
2464 // Check to make sure that all of the constants in the switch instruction
2465 // have the same type as the switched-on value.
2466 Type *SwitchTy = SI.getCondition()->getType();
2467 SmallPtrSet<ConstantInt*, 32> Constants;
2468 for (auto &Case : SI.cases()) {
2469 Assert(Case.getCaseValue()->getType() == SwitchTy,
2470 "Switch constants must all be same type as switch value!", &SI);
2471 Assert(Constants.insert(Case.getCaseValue()).second,
2472 "Duplicate integer as switch case", &SI, Case.getCaseValue());
2475 visitTerminator(SI);
2478 void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
2479 Assert(BI.getAddress()->getType()->isPointerTy(),
2480 "Indirectbr operand must have pointer type!", &BI);
2481 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
2482 Assert(BI.getDestination(i)->getType()->isLabelTy(),
2483 "Indirectbr destinations must all have pointer type!", &BI);
2485 visitTerminator(BI);
2488 void Verifier::visitCallBrInst(CallBrInst &CBI) {
2489 Assert(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!",
2490 &CBI);
2491 Assert(CBI.getType()->isVoidTy(), "Callbr return value is not supported!",
2492 &CBI);
2493 for (unsigned i = 0, e = CBI.getNumSuccessors(); i != e; ++i)
2494 Assert(CBI.getSuccessor(i)->getType()->isLabelTy(),
2495 "Callbr successors must all have pointer type!", &CBI);
2496 for (unsigned i = 0, e = CBI.getNumOperands(); i != e; ++i) {
2497 Assert(i >= CBI.getNumArgOperands() || !isa<BasicBlock>(CBI.getOperand(i)),
2498 "Using an unescaped label as a callbr argument!", &CBI);
2499 if (isa<BasicBlock>(CBI.getOperand(i)))
2500 for (unsigned j = i + 1; j != e; ++j)
2501 Assert(CBI.getOperand(i) != CBI.getOperand(j),
2502 "Duplicate callbr destination!", &CBI);
2505 visitTerminator(CBI);
2508 void Verifier::visitSelectInst(SelectInst &SI) {
2509 Assert(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
2510 SI.getOperand(2)),
2511 "Invalid operands for select instruction!", &SI);
2513 Assert(SI.getTrueValue()->getType() == SI.getType(),
2514 "Select values must have same type as select instruction!", &SI);
2515 visitInstruction(SI);
2518 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
2519 /// a pass, if any exist, it's an error.
2521 void Verifier::visitUserOp1(Instruction &I) {
2522 Assert(false, "User-defined operators should not live outside of a pass!", &I);
2525 void Verifier::visitTruncInst(TruncInst &I) {
2526 // Get the source and destination types
2527 Type *SrcTy = I.getOperand(0)->getType();
2528 Type *DestTy = I.getType();
2530 // Get the size of the types in bits, we'll need this later
2531 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2532 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2534 Assert(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
2535 Assert(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
2536 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2537 "trunc source and destination must both be a vector or neither", &I);
2538 Assert(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
2540 visitInstruction(I);
2543 void Verifier::visitZExtInst(ZExtInst &I) {
2544 // Get the source and destination types
2545 Type *SrcTy = I.getOperand(0)->getType();
2546 Type *DestTy = I.getType();
2548 // Get the size of the types in bits, we'll need this later
2549 Assert(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
2550 Assert(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
2551 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2552 "zext source and destination must both be a vector or neither", &I);
2553 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2554 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2556 Assert(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
2558 visitInstruction(I);
2561 void Verifier::visitSExtInst(SExtInst &I) {
2562 // Get the source and destination types
2563 Type *SrcTy = I.getOperand(0)->getType();
2564 Type *DestTy = I.getType();
2566 // Get the size of the types in bits, we'll need this later
2567 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2568 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2570 Assert(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
2571 Assert(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
2572 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2573 "sext source and destination must both be a vector or neither", &I);
2574 Assert(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
2576 visitInstruction(I);
2579 void Verifier::visitFPTruncInst(FPTruncInst &I) {
2580 // Get the source and destination types
2581 Type *SrcTy = I.getOperand(0)->getType();
2582 Type *DestTy = I.getType();
2583 // Get the size of the types in bits, we'll need this later
2584 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2585 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2587 Assert(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
2588 Assert(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
2589 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2590 "fptrunc source and destination must both be a vector or neither", &I);
2591 Assert(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
2593 visitInstruction(I);
2596 void Verifier::visitFPExtInst(FPExtInst &I) {
2597 // Get the source and destination types
2598 Type *SrcTy = I.getOperand(0)->getType();
2599 Type *DestTy = I.getType();
2601 // Get the size of the types in bits, we'll need this later
2602 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2603 unsigned DestBitSize = DestTy->getScalarSizeInBits();
2605 Assert(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
2606 Assert(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
2607 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2608 "fpext source and destination must both be a vector or neither", &I);
2609 Assert(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
2611 visitInstruction(I);
2614 void Verifier::visitUIToFPInst(UIToFPInst &I) {
2615 // Get the source and destination types
2616 Type *SrcTy = I.getOperand(0)->getType();
2617 Type *DestTy = I.getType();
2619 bool SrcVec = SrcTy->isVectorTy();
2620 bool DstVec = DestTy->isVectorTy();
2622 Assert(SrcVec == DstVec,
2623 "UIToFP source and dest must both be vector or scalar", &I);
2624 Assert(SrcTy->isIntOrIntVectorTy(),
2625 "UIToFP source must be integer or integer vector", &I);
2626 Assert(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
2627 &I);
2629 if (SrcVec && DstVec)
2630 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2631 cast<VectorType>(DestTy)->getNumElements(),
2632 "UIToFP source and dest vector length mismatch", &I);
2634 visitInstruction(I);
2637 void Verifier::visitSIToFPInst(SIToFPInst &I) {
2638 // Get the source and destination types
2639 Type *SrcTy = I.getOperand(0)->getType();
2640 Type *DestTy = I.getType();
2642 bool SrcVec = SrcTy->isVectorTy();
2643 bool DstVec = DestTy->isVectorTy();
2645 Assert(SrcVec == DstVec,
2646 "SIToFP source and dest must both be vector or scalar", &I);
2647 Assert(SrcTy->isIntOrIntVectorTy(),
2648 "SIToFP source must be integer or integer vector", &I);
2649 Assert(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
2650 &I);
2652 if (SrcVec && DstVec)
2653 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2654 cast<VectorType>(DestTy)->getNumElements(),
2655 "SIToFP source and dest vector length mismatch", &I);
2657 visitInstruction(I);
2660 void Verifier::visitFPToUIInst(FPToUIInst &I) {
2661 // Get the source and destination types
2662 Type *SrcTy = I.getOperand(0)->getType();
2663 Type *DestTy = I.getType();
2665 bool SrcVec = SrcTy->isVectorTy();
2666 bool DstVec = DestTy->isVectorTy();
2668 Assert(SrcVec == DstVec,
2669 "FPToUI source and dest must both be vector or scalar", &I);
2670 Assert(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
2671 &I);
2672 Assert(DestTy->isIntOrIntVectorTy(),
2673 "FPToUI result must be integer or integer vector", &I);
2675 if (SrcVec && DstVec)
2676 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2677 cast<VectorType>(DestTy)->getNumElements(),
2678 "FPToUI source and dest vector length mismatch", &I);
2680 visitInstruction(I);
2683 void Verifier::visitFPToSIInst(FPToSIInst &I) {
2684 // Get the source and destination types
2685 Type *SrcTy = I.getOperand(0)->getType();
2686 Type *DestTy = I.getType();
2688 bool SrcVec = SrcTy->isVectorTy();
2689 bool DstVec = DestTy->isVectorTy();
2691 Assert(SrcVec == DstVec,
2692 "FPToSI source and dest must both be vector or scalar", &I);
2693 Assert(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector",
2694 &I);
2695 Assert(DestTy->isIntOrIntVectorTy(),
2696 "FPToSI result must be integer or integer vector", &I);
2698 if (SrcVec && DstVec)
2699 Assert(cast<VectorType>(SrcTy)->getNumElements() ==
2700 cast<VectorType>(DestTy)->getNumElements(),
2701 "FPToSI source and dest vector length mismatch", &I);
2703 visitInstruction(I);
2706 void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
2707 // Get the source and destination types
2708 Type *SrcTy = I.getOperand(0)->getType();
2709 Type *DestTy = I.getType();
2711 Assert(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
2713 if (auto *PTy = dyn_cast<PointerType>(SrcTy->getScalarType()))
2714 Assert(!DL.isNonIntegralPointerType(PTy),
2715 "ptrtoint not supported for non-integral pointers");
2717 Assert(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
2718 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
2719 &I);
2721 if (SrcTy->isVectorTy()) {
2722 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
2723 VectorType *VDest = dyn_cast<VectorType>(DestTy);
2724 Assert(VSrc->getNumElements() == VDest->getNumElements(),
2725 "PtrToInt Vector width mismatch", &I);
2728 visitInstruction(I);
2731 void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
2732 // Get the source and destination types
2733 Type *SrcTy = I.getOperand(0)->getType();
2734 Type *DestTy = I.getType();
2736 Assert(SrcTy->isIntOrIntVectorTy(),
2737 "IntToPtr source must be an integral", &I);
2738 Assert(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
2740 if (auto *PTy = dyn_cast<PointerType>(DestTy->getScalarType()))
2741 Assert(!DL.isNonIntegralPointerType(PTy),
2742 "inttoptr not supported for non-integral pointers");
2744 Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
2745 &I);
2746 if (SrcTy->isVectorTy()) {
2747 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
2748 VectorType *VDest = dyn_cast<VectorType>(DestTy);
2749 Assert(VSrc->getNumElements() == VDest->getNumElements(),
2750 "IntToPtr Vector width mismatch", &I);
2752 visitInstruction(I);
2755 void Verifier::visitBitCastInst(BitCastInst &I) {
2756 Assert(
2757 CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
2758 "Invalid bitcast", &I);
2759 visitInstruction(I);
2762 void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
2763 Type *SrcTy = I.getOperand(0)->getType();
2764 Type *DestTy = I.getType();
2766 Assert(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
2767 &I);
2768 Assert(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
2769 &I);
2770 Assert(SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace(),
2771 "AddrSpaceCast must be between different address spaces", &I);
2772 if (SrcTy->isVectorTy())
2773 Assert(SrcTy->getVectorNumElements() == DestTy->getVectorNumElements(),
2774 "AddrSpaceCast vector pointer number of elements mismatch", &I);
2775 visitInstruction(I);
2778 /// visitPHINode - Ensure that a PHI node is well formed.
2780 void Verifier::visitPHINode(PHINode &PN) {
2781 // Ensure that the PHI nodes are all grouped together at the top of the block.
2782 // This can be tested by checking whether the instruction before this is
2783 // either nonexistent (because this is begin()) or is a PHI node. If not,
2784 // then there is some other instruction before a PHI.
2785 Assert(&PN == &PN.getParent()->front() ||
2786 isa<PHINode>(--BasicBlock::iterator(&PN)),
2787 "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
2789 // Check that a PHI doesn't yield a Token.
2790 Assert(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
2792 // Check that all of the values of the PHI node have the same type as the
2793 // result, and that the incoming blocks are really basic blocks.
2794 for (Value *IncValue : PN.incoming_values()) {
2795 Assert(PN.getType() == IncValue->getType(),
2796 "PHI node operands are not the same type as the result!", &PN);
2799 // All other PHI node constraints are checked in the visitBasicBlock method.
2801 visitInstruction(PN);
2804 void Verifier::visitCallBase(CallBase &Call) {
2805 Assert(Call.getCalledValue()->getType()->isPointerTy(),
2806 "Called function must be a pointer!", Call);
2807 PointerType *FPTy = cast<PointerType>(Call.getCalledValue()->getType());
2809 Assert(FPTy->getElementType()->isFunctionTy(),
2810 "Called function is not pointer to function type!", Call);
2812 Assert(FPTy->getElementType() == Call.getFunctionType(),
2813 "Called function is not the same type as the call!", Call);
2815 FunctionType *FTy = Call.getFunctionType();
2817 // Verify that the correct number of arguments are being passed
2818 if (FTy->isVarArg())
2819 Assert(Call.arg_size() >= FTy->getNumParams(),
2820 "Called function requires more parameters than were provided!",
2821 Call);
2822 else
2823 Assert(Call.arg_size() == FTy->getNumParams(),
2824 "Incorrect number of arguments passed to called function!", Call);
2826 // Verify that all arguments to the call match the function type.
2827 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
2828 Assert(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
2829 "Call parameter type does not match function signature!",
2830 Call.getArgOperand(i), FTy->getParamType(i), Call);
2832 AttributeList Attrs = Call.getAttributes();
2834 Assert(verifyAttributeCount(Attrs, Call.arg_size()),
2835 "Attribute after last parameter!", Call);
2837 bool IsIntrinsic = Call.getCalledFunction() &&
2838 Call.getCalledFunction()->getName().startswith("llvm.");
2840 Function *Callee
2841 = dyn_cast<Function>(Call.getCalledValue()->stripPointerCasts());
2843 if (Attrs.hasAttribute(AttributeList::FunctionIndex, Attribute::Speculatable)) {
2844 // Don't allow speculatable on call sites, unless the underlying function
2845 // declaration is also speculatable.
2846 Assert(Callee && Callee->isSpeculatable(),
2847 "speculatable attribute may not apply to call sites", Call);
2850 // Verify call attributes.
2851 verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic);
2853 // Conservatively check the inalloca argument.
2854 // We have a bug if we can find that there is an underlying alloca without
2855 // inalloca.
2856 if (Call.hasInAllocaArgument()) {
2857 Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
2858 if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
2859 Assert(AI->isUsedWithInAlloca(),
2860 "inalloca argument for call has mismatched alloca", AI, Call);
2863 // For each argument of the callsite, if it has the swifterror argument,
2864 // make sure the underlying alloca/parameter it comes from has a swifterror as
2865 // well.
2866 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
2867 if (Call.paramHasAttr(i, Attribute::SwiftError)) {
2868 Value *SwiftErrorArg = Call.getArgOperand(i);
2869 if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
2870 Assert(AI->isSwiftError(),
2871 "swifterror argument for call has mismatched alloca", AI, Call);
2872 continue;
2874 auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
2875 Assert(ArgI,
2876 "swifterror argument should come from an alloca or parameter",
2877 SwiftErrorArg, Call);
2878 Assert(ArgI->hasSwiftErrorAttr(),
2879 "swifterror argument for call has mismatched parameter", ArgI,
2880 Call);
2883 if (Attrs.hasParamAttribute(i, Attribute::ImmArg)) {
2884 // Don't allow immarg on call sites, unless the underlying declaration
2885 // also has the matching immarg.
2886 Assert(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
2887 "immarg may not apply only to call sites",
2888 Call.getArgOperand(i), Call);
2891 if (Call.paramHasAttr(i, Attribute::ImmArg)) {
2892 Value *ArgVal = Call.getArgOperand(i);
2893 Assert(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
2894 "immarg operand has non-immediate parameter", ArgVal, Call);
2898 if (FTy->isVarArg()) {
2899 // FIXME? is 'nest' even legal here?
2900 bool SawNest = false;
2901 bool SawReturned = false;
2903 for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
2904 if (Attrs.hasParamAttribute(Idx, Attribute::Nest))
2905 SawNest = true;
2906 if (Attrs.hasParamAttribute(Idx, Attribute::Returned))
2907 SawReturned = true;
2910 // Check attributes on the varargs part.
2911 for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
2912 Type *Ty = Call.getArgOperand(Idx)->getType();
2913 AttributeSet ArgAttrs = Attrs.getParamAttributes(Idx);
2914 verifyParameterAttrs(ArgAttrs, Ty, &Call);
2916 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
2917 Assert(!SawNest, "More than one parameter has attribute nest!", Call);
2918 SawNest = true;
2921 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
2922 Assert(!SawReturned, "More than one parameter has attribute returned!",
2923 Call);
2924 Assert(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
2925 "Incompatible argument and return types for 'returned' "
2926 "attribute",
2927 Call);
2928 SawReturned = true;
2931 // Statepoint intrinsic is vararg but the wrapped function may be not.
2932 // Allow sret here and check the wrapped function in verifyStatepoint.
2933 if (!Call.getCalledFunction() ||
2934 Call.getCalledFunction()->getIntrinsicID() !=
2935 Intrinsic::experimental_gc_statepoint)
2936 Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
2937 "Attribute 'sret' cannot be used for vararg call arguments!",
2938 Call);
2940 if (ArgAttrs.hasAttribute(Attribute::InAlloca))
2941 Assert(Idx == Call.arg_size() - 1,
2942 "inalloca isn't on the last argument!", Call);
2946 // Verify that there's no metadata unless it's a direct call to an intrinsic.
2947 if (!IsIntrinsic) {
2948 for (Type *ParamTy : FTy->params()) {
2949 Assert(!ParamTy->isMetadataTy(),
2950 "Function has metadata parameter but isn't an intrinsic", Call);
2951 Assert(!ParamTy->isTokenTy(),
2952 "Function has token parameter but isn't an intrinsic", Call);
2956 // Verify that indirect calls don't return tokens.
2957 if (!Call.getCalledFunction())
2958 Assert(!FTy->getReturnType()->isTokenTy(),
2959 "Return type cannot be token for indirect call!");
2961 if (Function *F = Call.getCalledFunction())
2962 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
2963 visitIntrinsicCall(ID, Call);
2965 // Verify that a callsite has at most one "deopt", at most one "funclet" and
2966 // at most one "gc-transition" operand bundle.
2967 bool FoundDeoptBundle = false, FoundFuncletBundle = false,
2968 FoundGCTransitionBundle = false;
2969 for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
2970 OperandBundleUse BU = Call.getOperandBundleAt(i);
2971 uint32_t Tag = BU.getTagID();
2972 if (Tag == LLVMContext::OB_deopt) {
2973 Assert(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
2974 FoundDeoptBundle = true;
2975 } else if (Tag == LLVMContext::OB_gc_transition) {
2976 Assert(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
2977 Call);
2978 FoundGCTransitionBundle = true;
2979 } else if (Tag == LLVMContext::OB_funclet) {
2980 Assert(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
2981 FoundFuncletBundle = true;
2982 Assert(BU.Inputs.size() == 1,
2983 "Expected exactly one funclet bundle operand", Call);
2984 Assert(isa<FuncletPadInst>(BU.Inputs.front()),
2985 "Funclet bundle operands should correspond to a FuncletPadInst",
2986 Call);
2990 // Verify that each inlinable callsite of a debug-info-bearing function in a
2991 // debug-info-bearing function has a debug location attached to it. Failure to
2992 // do so causes assertion failures when the inliner sets up inline scope info.
2993 if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
2994 Call.getCalledFunction()->getSubprogram())
2995 AssertDI(Call.getDebugLoc(),
2996 "inlinable function call in a function with "
2997 "debug info must have a !dbg location",
2998 Call);
3000 visitInstruction(Call);
3003 /// Two types are "congruent" if they are identical, or if they are both pointer
3004 /// types with different pointee types and the same address space.
3005 static bool isTypeCongruent(Type *L, Type *R) {
3006 if (L == R)
3007 return true;
3008 PointerType *PL = dyn_cast<PointerType>(L);
3009 PointerType *PR = dyn_cast<PointerType>(R);
3010 if (!PL || !PR)
3011 return false;
3012 return PL->getAddressSpace() == PR->getAddressSpace();
3015 static AttrBuilder getParameterABIAttributes(int I, AttributeList Attrs) {
3016 static const Attribute::AttrKind ABIAttrs[] = {
3017 Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
3018 Attribute::InReg, Attribute::Returned, Attribute::SwiftSelf,
3019 Attribute::SwiftError};
3020 AttrBuilder Copy;
3021 for (auto AK : ABIAttrs) {
3022 if (Attrs.hasParamAttribute(I, AK))
3023 Copy.addAttribute(AK);
3025 if (Attrs.hasParamAttribute(I, Attribute::Alignment))
3026 Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
3027 return Copy;
3030 void Verifier::verifyMustTailCall(CallInst &CI) {
3031 Assert(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
3033 // - The caller and callee prototypes must match. Pointer types of
3034 // parameters or return types may differ in pointee type, but not
3035 // address space.
3036 Function *F = CI.getParent()->getParent();
3037 FunctionType *CallerTy = F->getFunctionType();
3038 FunctionType *CalleeTy = CI.getFunctionType();
3039 if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
3040 Assert(CallerTy->getNumParams() == CalleeTy->getNumParams(),
3041 "cannot guarantee tail call due to mismatched parameter counts",
3042 &CI);
3043 for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3044 Assert(
3045 isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
3046 "cannot guarantee tail call due to mismatched parameter types", &CI);
3049 Assert(CallerTy->isVarArg() == CalleeTy->isVarArg(),
3050 "cannot guarantee tail call due to mismatched varargs", &CI);
3051 Assert(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
3052 "cannot guarantee tail call due to mismatched return types", &CI);
3054 // - The calling conventions of the caller and callee must match.
3055 Assert(F->getCallingConv() == CI.getCallingConv(),
3056 "cannot guarantee tail call due to mismatched calling conv", &CI);
3058 // - All ABI-impacting function attributes, such as sret, byval, inreg,
3059 // returned, and inalloca, must match.
3060 AttributeList CallerAttrs = F->getAttributes();
3061 AttributeList CalleeAttrs = CI.getAttributes();
3062 for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3063 AttrBuilder CallerABIAttrs = getParameterABIAttributes(I, CallerAttrs);
3064 AttrBuilder CalleeABIAttrs = getParameterABIAttributes(I, CalleeAttrs);
3065 Assert(CallerABIAttrs == CalleeABIAttrs,
3066 "cannot guarantee tail call due to mismatched ABI impacting "
3067 "function attributes",
3068 &CI, CI.getOperand(I));
3071 // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
3072 // or a pointer bitcast followed by a ret instruction.
3073 // - The ret instruction must return the (possibly bitcasted) value
3074 // produced by the call or void.
3075 Value *RetVal = &CI;
3076 Instruction *Next = CI.getNextNode();
3078 // Handle the optional bitcast.
3079 if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
3080 Assert(BI->getOperand(0) == RetVal,
3081 "bitcast following musttail call must use the call", BI);
3082 RetVal = BI;
3083 Next = BI->getNextNode();
3086 // Check the return.
3087 ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
3088 Assert(Ret, "musttail call must precede a ret with an optional bitcast",
3089 &CI);
3090 Assert(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal,
3091 "musttail call result must be returned", Ret);
3094 void Verifier::visitCallInst(CallInst &CI) {
3095 visitCallBase(CI);
3097 if (CI.isMustTailCall())
3098 verifyMustTailCall(CI);
3101 void Verifier::visitInvokeInst(InvokeInst &II) {
3102 visitCallBase(II);
3104 // Verify that the first non-PHI instruction of the unwind destination is an
3105 // exception handling instruction.
3106 Assert(
3107 II.getUnwindDest()->isEHPad(),
3108 "The unwind destination does not have an exception handling instruction!",
3109 &II);
3111 visitTerminator(II);
3114 /// visitUnaryOperator - Check the argument to the unary operator.
3116 void Verifier::visitUnaryOperator(UnaryOperator &U) {
3117 Assert(U.getType() == U.getOperand(0)->getType(),
3118 "Unary operators must have same type for"
3119 "operands and result!",
3120 &U);
3122 switch (U.getOpcode()) {
3123 // Check that floating-point arithmetic operators are only used with
3124 // floating-point operands.
3125 case Instruction::FNeg:
3126 Assert(U.getType()->isFPOrFPVectorTy(),
3127 "FNeg operator only works with float types!", &U);
3128 break;
3129 default:
3130 llvm_unreachable("Unknown UnaryOperator opcode!");
3133 visitInstruction(U);
3136 /// visitBinaryOperator - Check that both arguments to the binary operator are
3137 /// of the same type!
3139 void Verifier::visitBinaryOperator(BinaryOperator &B) {
3140 Assert(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
3141 "Both operands to a binary operator are not of the same type!", &B);
3143 switch (B.getOpcode()) {
3144 // Check that integer arithmetic operators are only used with
3145 // integral operands.
3146 case Instruction::Add:
3147 case Instruction::Sub:
3148 case Instruction::Mul:
3149 case Instruction::SDiv:
3150 case Instruction::UDiv:
3151 case Instruction::SRem:
3152 case Instruction::URem:
3153 Assert(B.getType()->isIntOrIntVectorTy(),
3154 "Integer arithmetic operators only work with integral types!", &B);
3155 Assert(B.getType() == B.getOperand(0)->getType(),
3156 "Integer arithmetic operators must have same type "
3157 "for operands and result!",
3158 &B);
3159 break;
3160 // Check that floating-point arithmetic operators are only used with
3161 // floating-point operands.
3162 case Instruction::FAdd:
3163 case Instruction::FSub:
3164 case Instruction::FMul:
3165 case Instruction::FDiv:
3166 case Instruction::FRem:
3167 Assert(B.getType()->isFPOrFPVectorTy(),
3168 "Floating-point arithmetic operators only work with "
3169 "floating-point types!",
3170 &B);
3171 Assert(B.getType() == B.getOperand(0)->getType(),
3172 "Floating-point arithmetic operators must have same type "
3173 "for operands and result!",
3174 &B);
3175 break;
3176 // Check that logical operators are only used with integral operands.
3177 case Instruction::And:
3178 case Instruction::Or:
3179 case Instruction::Xor:
3180 Assert(B.getType()->isIntOrIntVectorTy(),
3181 "Logical operators only work with integral types!", &B);
3182 Assert(B.getType() == B.getOperand(0)->getType(),
3183 "Logical operators must have same type for operands and result!",
3184 &B);
3185 break;
3186 case Instruction::Shl:
3187 case Instruction::LShr:
3188 case Instruction::AShr:
3189 Assert(B.getType()->isIntOrIntVectorTy(),
3190 "Shifts only work with integral types!", &B);
3191 Assert(B.getType() == B.getOperand(0)->getType(),
3192 "Shift return type must be same as operands!", &B);
3193 break;
3194 default:
3195 llvm_unreachable("Unknown BinaryOperator opcode!");
3198 visitInstruction(B);
3201 void Verifier::visitICmpInst(ICmpInst &IC) {
3202 // Check that the operands are the same type
3203 Type *Op0Ty = IC.getOperand(0)->getType();
3204 Type *Op1Ty = IC.getOperand(1)->getType();
3205 Assert(Op0Ty == Op1Ty,
3206 "Both operands to ICmp instruction are not of the same type!", &IC);
3207 // Check that the operands are the right type
3208 Assert(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
3209 "Invalid operand types for ICmp instruction", &IC);
3210 // Check that the predicate is valid.
3211 Assert(IC.isIntPredicate(),
3212 "Invalid predicate in ICmp instruction!", &IC);
3214 visitInstruction(IC);
3217 void Verifier::visitFCmpInst(FCmpInst &FC) {
3218 // Check that the operands are the same type
3219 Type *Op0Ty = FC.getOperand(0)->getType();
3220 Type *Op1Ty = FC.getOperand(1)->getType();
3221 Assert(Op0Ty == Op1Ty,
3222 "Both operands to FCmp instruction are not of the same type!", &FC);
3223 // Check that the operands are the right type
3224 Assert(Op0Ty->isFPOrFPVectorTy(),
3225 "Invalid operand types for FCmp instruction", &FC);
3226 // Check that the predicate is valid.
3227 Assert(FC.isFPPredicate(),
3228 "Invalid predicate in FCmp instruction!", &FC);
3230 visitInstruction(FC);
3233 void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
3234 Assert(
3235 ExtractElementInst::isValidOperands(EI.getOperand(0), EI.getOperand(1)),
3236 "Invalid extractelement operands!", &EI);
3237 visitInstruction(EI);
3240 void Verifier::visitInsertElementInst(InsertElementInst &IE) {
3241 Assert(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
3242 IE.getOperand(2)),
3243 "Invalid insertelement operands!", &IE);
3244 visitInstruction(IE);
3247 void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
3248 Assert(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
3249 SV.getOperand(2)),
3250 "Invalid shufflevector operands!", &SV);
3251 visitInstruction(SV);
3254 void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
3255 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
3257 Assert(isa<PointerType>(TargetTy),
3258 "GEP base pointer is not a vector or a vector of pointers", &GEP);
3259 Assert(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
3261 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
3262 Assert(all_of(
3263 Idxs, [](Value* V) { return V->getType()->isIntOrIntVectorTy(); }),
3264 "GEP indexes must be integers", &GEP);
3265 Type *ElTy =
3266 GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
3267 Assert(ElTy, "Invalid indices for GEP pointer type!", &GEP);
3269 Assert(GEP.getType()->isPtrOrPtrVectorTy() &&
3270 GEP.getResultElementType() == ElTy,
3271 "GEP is not of right type for indices!", &GEP, ElTy);
3273 if (GEP.getType()->isVectorTy()) {
3274 // Additional checks for vector GEPs.
3275 unsigned GEPWidth = GEP.getType()->getVectorNumElements();
3276 if (GEP.getPointerOperandType()->isVectorTy())
3277 Assert(GEPWidth == GEP.getPointerOperandType()->getVectorNumElements(),
3278 "Vector GEP result width doesn't match operand's", &GEP);
3279 for (Value *Idx : Idxs) {
3280 Type *IndexTy = Idx->getType();
3281 if (IndexTy->isVectorTy()) {
3282 unsigned IndexWidth = IndexTy->getVectorNumElements();
3283 Assert(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
3285 Assert(IndexTy->isIntOrIntVectorTy(),
3286 "All GEP indices should be of integer type");
3290 if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
3291 Assert(GEP.getAddressSpace() == PTy->getAddressSpace(),
3292 "GEP address space doesn't match type", &GEP);
3295 visitInstruction(GEP);
3298 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
3299 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
3302 void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
3303 assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
3304 "precondition violation");
3306 unsigned NumOperands = Range->getNumOperands();
3307 Assert(NumOperands % 2 == 0, "Unfinished range!", Range);
3308 unsigned NumRanges = NumOperands / 2;
3309 Assert(NumRanges >= 1, "It should have at least one range!", Range);
3311 ConstantRange LastRange(1, true); // Dummy initial value
3312 for (unsigned i = 0; i < NumRanges; ++i) {
3313 ConstantInt *Low =
3314 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
3315 Assert(Low, "The lower limit must be an integer!", Low);
3316 ConstantInt *High =
3317 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
3318 Assert(High, "The upper limit must be an integer!", High);
3319 Assert(High->getType() == Low->getType() && High->getType() == Ty,
3320 "Range types must match instruction type!", &I);
3322 APInt HighV = High->getValue();
3323 APInt LowV = Low->getValue();
3324 ConstantRange CurRange(LowV, HighV);
3325 Assert(!CurRange.isEmptySet() && !CurRange.isFullSet(),
3326 "Range must not be empty!", Range);
3327 if (i != 0) {
3328 Assert(CurRange.intersectWith(LastRange).isEmptySet(),
3329 "Intervals are overlapping", Range);
3330 Assert(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
3331 Range);
3332 Assert(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
3333 Range);
3335 LastRange = ConstantRange(LowV, HighV);
3337 if (NumRanges > 2) {
3338 APInt FirstLow =
3339 mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
3340 APInt FirstHigh =
3341 mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
3342 ConstantRange FirstRange(FirstLow, FirstHigh);
3343 Assert(FirstRange.intersectWith(LastRange).isEmptySet(),
3344 "Intervals are overlapping", Range);
3345 Assert(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
3346 Range);
3350 void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
3351 unsigned Size = DL.getTypeSizeInBits(Ty);
3352 Assert(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
3353 Assert(!(Size & (Size - 1)),
3354 "atomic memory access' operand must have a power-of-two size", Ty, I);
3357 void Verifier::visitLoadInst(LoadInst &LI) {
3358 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
3359 Assert(PTy, "Load operand must be a pointer.", &LI);
3360 Type *ElTy = LI.getType();
3361 Assert(LI.getAlignment() <= Value::MaximumAlignment,
3362 "huge alignment values are unsupported", &LI);
3363 Assert(ElTy->isSized(), "loading unsized types is not allowed", &LI);
3364 if (LI.isAtomic()) {
3365 Assert(LI.getOrdering() != AtomicOrdering::Release &&
3366 LI.getOrdering() != AtomicOrdering::AcquireRelease,
3367 "Load cannot have Release ordering", &LI);
3368 Assert(LI.getAlignment() != 0,
3369 "Atomic load must specify explicit alignment", &LI);
3370 Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3371 "atomic load operand must have integer, pointer, or floating point "
3372 "type!",
3373 ElTy, &LI);
3374 checkAtomicMemAccessSize(ElTy, &LI);
3375 } else {
3376 Assert(LI.getSyncScopeID() == SyncScope::System,
3377 "Non-atomic load cannot have SynchronizationScope specified", &LI);
3380 visitInstruction(LI);
3383 void Verifier::visitStoreInst(StoreInst &SI) {
3384 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
3385 Assert(PTy, "Store operand must be a pointer.", &SI);
3386 Type *ElTy = PTy->getElementType();
3387 Assert(ElTy == SI.getOperand(0)->getType(),
3388 "Stored value type does not match pointer operand type!", &SI, ElTy);
3389 Assert(SI.getAlignment() <= Value::MaximumAlignment,
3390 "huge alignment values are unsupported", &SI);
3391 Assert(ElTy->isSized(), "storing unsized types is not allowed", &SI);
3392 if (SI.isAtomic()) {
3393 Assert(SI.getOrdering() != AtomicOrdering::Acquire &&
3394 SI.getOrdering() != AtomicOrdering::AcquireRelease,
3395 "Store cannot have Acquire ordering", &SI);
3396 Assert(SI.getAlignment() != 0,
3397 "Atomic store must specify explicit alignment", &SI);
3398 Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3399 "atomic store operand must have integer, pointer, or floating point "
3400 "type!",
3401 ElTy, &SI);
3402 checkAtomicMemAccessSize(ElTy, &SI);
3403 } else {
3404 Assert(SI.getSyncScopeID() == SyncScope::System,
3405 "Non-atomic store cannot have SynchronizationScope specified", &SI);
3407 visitInstruction(SI);
3410 /// Check that SwiftErrorVal is used as a swifterror argument in CS.
3411 void Verifier::verifySwiftErrorCall(CallBase &Call,
3412 const Value *SwiftErrorVal) {
3413 unsigned Idx = 0;
3414 for (auto I = Call.arg_begin(), E = Call.arg_end(); I != E; ++I, ++Idx) {
3415 if (*I == SwiftErrorVal) {
3416 Assert(Call.paramHasAttr(Idx, Attribute::SwiftError),
3417 "swifterror value when used in a callsite should be marked "
3418 "with swifterror attribute",
3419 SwiftErrorVal, Call);
3424 void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
3425 // Check that swifterror value is only used by loads, stores, or as
3426 // a swifterror argument.
3427 for (const User *U : SwiftErrorVal->users()) {
3428 Assert(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
3429 isa<InvokeInst>(U),
3430 "swifterror value can only be loaded and stored from, or "
3431 "as a swifterror argument!",
3432 SwiftErrorVal, U);
3433 // If it is used by a store, check it is the second operand.
3434 if (auto StoreI = dyn_cast<StoreInst>(U))
3435 Assert(StoreI->getOperand(1) == SwiftErrorVal,
3436 "swifterror value should be the second operand when used "
3437 "by stores", SwiftErrorVal, U);
3438 if (auto *Call = dyn_cast<CallBase>(U))
3439 verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
3443 void Verifier::visitAllocaInst(AllocaInst &AI) {
3444 SmallPtrSet<Type*, 4> Visited;
3445 PointerType *PTy = AI.getType();
3446 // TODO: Relax this restriction?
3447 Assert(PTy->getAddressSpace() == DL.getAllocaAddrSpace(),
3448 "Allocation instruction pointer not in the stack address space!",
3449 &AI);
3450 Assert(AI.getAllocatedType()->isSized(&Visited),
3451 "Cannot allocate unsized type", &AI);
3452 Assert(AI.getArraySize()->getType()->isIntegerTy(),
3453 "Alloca array size must have integer type", &AI);
3454 Assert(AI.getAlignment() <= Value::MaximumAlignment,
3455 "huge alignment values are unsupported", &AI);
3457 if (AI.isSwiftError()) {
3458 verifySwiftErrorValue(&AI);
3461 visitInstruction(AI);
3464 void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
3466 // FIXME: more conditions???
3467 Assert(CXI.getSuccessOrdering() != AtomicOrdering::NotAtomic,
3468 "cmpxchg instructions must be atomic.", &CXI);
3469 Assert(CXI.getFailureOrdering() != AtomicOrdering::NotAtomic,
3470 "cmpxchg instructions must be atomic.", &CXI);
3471 Assert(CXI.getSuccessOrdering() != AtomicOrdering::Unordered,
3472 "cmpxchg instructions cannot be unordered.", &CXI);
3473 Assert(CXI.getFailureOrdering() != AtomicOrdering::Unordered,
3474 "cmpxchg instructions cannot be unordered.", &CXI);
3475 Assert(!isStrongerThan(CXI.getFailureOrdering(), CXI.getSuccessOrdering()),
3476 "cmpxchg instructions failure argument shall be no stronger than the "
3477 "success argument",
3478 &CXI);
3479 Assert(CXI.getFailureOrdering() != AtomicOrdering::Release &&
3480 CXI.getFailureOrdering() != AtomicOrdering::AcquireRelease,
3481 "cmpxchg failure ordering cannot include release semantics", &CXI);
3483 PointerType *PTy = dyn_cast<PointerType>(CXI.getOperand(0)->getType());
3484 Assert(PTy, "First cmpxchg operand must be a pointer.", &CXI);
3485 Type *ElTy = PTy->getElementType();
3486 Assert(ElTy->isIntOrPtrTy(),
3487 "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
3488 checkAtomicMemAccessSize(ElTy, &CXI);
3489 Assert(ElTy == CXI.getOperand(1)->getType(),
3490 "Expected value type does not match pointer operand type!", &CXI,
3491 ElTy);
3492 Assert(ElTy == CXI.getOperand(2)->getType(),
3493 "Stored value type does not match pointer operand type!", &CXI, ElTy);
3494 visitInstruction(CXI);
3497 void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
3498 Assert(RMWI.getOrdering() != AtomicOrdering::NotAtomic,
3499 "atomicrmw instructions must be atomic.", &RMWI);
3500 Assert(RMWI.getOrdering() != AtomicOrdering::Unordered,
3501 "atomicrmw instructions cannot be unordered.", &RMWI);
3502 auto Op = RMWI.getOperation();
3503 PointerType *PTy = dyn_cast<PointerType>(RMWI.getOperand(0)->getType());
3504 Assert(PTy, "First atomicrmw operand must be a pointer.", &RMWI);
3505 Type *ElTy = PTy->getElementType();
3506 if (Op == AtomicRMWInst::Xchg) {
3507 Assert(ElTy->isIntegerTy() || ElTy->isFloatingPointTy(), "atomicrmw " +
3508 AtomicRMWInst::getOperationName(Op) +
3509 " operand must have integer or floating point type!",
3510 &RMWI, ElTy);
3511 } else if (AtomicRMWInst::isFPOperation(Op)) {
3512 Assert(ElTy->isFloatingPointTy(), "atomicrmw " +
3513 AtomicRMWInst::getOperationName(Op) +
3514 " operand must have floating point type!",
3515 &RMWI, ElTy);
3516 } else {
3517 Assert(ElTy->isIntegerTy(), "atomicrmw " +
3518 AtomicRMWInst::getOperationName(Op) +
3519 " operand must have integer type!",
3520 &RMWI, ElTy);
3522 checkAtomicMemAccessSize(ElTy, &RMWI);
3523 Assert(ElTy == RMWI.getOperand(1)->getType(),
3524 "Argument value type does not match pointer operand type!", &RMWI,
3525 ElTy);
3526 Assert(AtomicRMWInst::FIRST_BINOP <= Op && Op <= AtomicRMWInst::LAST_BINOP,
3527 "Invalid binary operation!", &RMWI);
3528 visitInstruction(RMWI);
3531 void Verifier::visitFenceInst(FenceInst &FI) {
3532 const AtomicOrdering Ordering = FI.getOrdering();
3533 Assert(Ordering == AtomicOrdering::Acquire ||
3534 Ordering == AtomicOrdering::Release ||
3535 Ordering == AtomicOrdering::AcquireRelease ||
3536 Ordering == AtomicOrdering::SequentiallyConsistent,
3537 "fence instructions may only have acquire, release, acq_rel, or "
3538 "seq_cst ordering.",
3539 &FI);
3540 visitInstruction(FI);
3543 void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
3544 Assert(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
3545 EVI.getIndices()) == EVI.getType(),
3546 "Invalid ExtractValueInst operands!", &EVI);
3548 visitInstruction(EVI);
3551 void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
3552 Assert(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
3553 IVI.getIndices()) ==
3554 IVI.getOperand(1)->getType(),
3555 "Invalid InsertValueInst operands!", &IVI);
3557 visitInstruction(IVI);
3560 static Value *getParentPad(Value *EHPad) {
3561 if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
3562 return FPI->getParentPad();
3564 return cast<CatchSwitchInst>(EHPad)->getParentPad();
3567 void Verifier::visitEHPadPredecessors(Instruction &I) {
3568 assert(I.isEHPad());
3570 BasicBlock *BB = I.getParent();
3571 Function *F = BB->getParent();
3573 Assert(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
3575 if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
3576 // The landingpad instruction defines its parent as a landing pad block. The
3577 // landing pad block may be branched to only by the unwind edge of an
3578 // invoke.
3579 for (BasicBlock *PredBB : predecessors(BB)) {
3580 const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
3581 Assert(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
3582 "Block containing LandingPadInst must be jumped to "
3583 "only by the unwind edge of an invoke.",
3584 LPI);
3586 return;
3588 if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
3589 if (!pred_empty(BB))
3590 Assert(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
3591 "Block containg CatchPadInst must be jumped to "
3592 "only by its catchswitch.",
3593 CPI);
3594 Assert(BB != CPI->getCatchSwitch()->getUnwindDest(),
3595 "Catchswitch cannot unwind to one of its catchpads",
3596 CPI->getCatchSwitch(), CPI);
3597 return;
3600 // Verify that each pred has a legal terminator with a legal to/from EH
3601 // pad relationship.
3602 Instruction *ToPad = &I;
3603 Value *ToPadParent = getParentPad(ToPad);
3604 for (BasicBlock *PredBB : predecessors(BB)) {
3605 Instruction *TI = PredBB->getTerminator();
3606 Value *FromPad;
3607 if (auto *II = dyn_cast<InvokeInst>(TI)) {
3608 Assert(II->getUnwindDest() == BB && II->getNormalDest() != BB,
3609 "EH pad must be jumped to via an unwind edge", ToPad, II);
3610 if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
3611 FromPad = Bundle->Inputs[0];
3612 else
3613 FromPad = ConstantTokenNone::get(II->getContext());
3614 } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
3615 FromPad = CRI->getOperand(0);
3616 Assert(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
3617 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
3618 FromPad = CSI;
3619 } else {
3620 Assert(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
3623 // The edge may exit from zero or more nested pads.
3624 SmallSet<Value *, 8> Seen;
3625 for (;; FromPad = getParentPad(FromPad)) {
3626 Assert(FromPad != ToPad,
3627 "EH pad cannot handle exceptions raised within it", FromPad, TI);
3628 if (FromPad == ToPadParent) {
3629 // This is a legal unwind edge.
3630 break;
3632 Assert(!isa<ConstantTokenNone>(FromPad),
3633 "A single unwind edge may only enter one EH pad", TI);
3634 Assert(Seen.insert(FromPad).second,
3635 "EH pad jumps through a cycle of pads", FromPad);
3640 void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
3641 // The landingpad instruction is ill-formed if it doesn't have any clauses and
3642 // isn't a cleanup.
3643 Assert(LPI.getNumClauses() > 0 || LPI.isCleanup(),
3644 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
3646 visitEHPadPredecessors(LPI);
3648 if (!LandingPadResultTy)
3649 LandingPadResultTy = LPI.getType();
3650 else
3651 Assert(LandingPadResultTy == LPI.getType(),
3652 "The landingpad instruction should have a consistent result type "
3653 "inside a function.",
3654 &LPI);
3656 Function *F = LPI.getParent()->getParent();
3657 Assert(F->hasPersonalityFn(),
3658 "LandingPadInst needs to be in a function with a personality.", &LPI);
3660 // The landingpad instruction must be the first non-PHI instruction in the
3661 // block.
3662 Assert(LPI.getParent()->getLandingPadInst() == &LPI,
3663 "LandingPadInst not the first non-PHI instruction in the block.",
3664 &LPI);
3666 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
3667 Constant *Clause = LPI.getClause(i);
3668 if (LPI.isCatch(i)) {
3669 Assert(isa<PointerType>(Clause->getType()),
3670 "Catch operand does not have pointer type!", &LPI);
3671 } else {
3672 Assert(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
3673 Assert(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
3674 "Filter operand is not an array of constants!", &LPI);
3678 visitInstruction(LPI);
3681 void Verifier::visitResumeInst(ResumeInst &RI) {
3682 Assert(RI.getFunction()->hasPersonalityFn(),
3683 "ResumeInst needs to be in a function with a personality.", &RI);
3685 if (!LandingPadResultTy)
3686 LandingPadResultTy = RI.getValue()->getType();
3687 else
3688 Assert(LandingPadResultTy == RI.getValue()->getType(),
3689 "The resume instruction should have a consistent result type "
3690 "inside a function.",
3691 &RI);
3693 visitTerminator(RI);
3696 void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
3697 BasicBlock *BB = CPI.getParent();
3699 Function *F = BB->getParent();
3700 Assert(F->hasPersonalityFn(),
3701 "CatchPadInst needs to be in a function with a personality.", &CPI);
3703 Assert(isa<CatchSwitchInst>(CPI.getParentPad()),
3704 "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
3705 CPI.getParentPad());
3707 // The catchpad instruction must be the first non-PHI instruction in the
3708 // block.
3709 Assert(BB->getFirstNonPHI() == &CPI,
3710 "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
3712 visitEHPadPredecessors(CPI);
3713 visitFuncletPadInst(CPI);
3716 void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
3717 Assert(isa<CatchPadInst>(CatchReturn.getOperand(0)),
3718 "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
3719 CatchReturn.getOperand(0));
3721 visitTerminator(CatchReturn);
3724 void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
3725 BasicBlock *BB = CPI.getParent();
3727 Function *F = BB->getParent();
3728 Assert(F->hasPersonalityFn(),
3729 "CleanupPadInst needs to be in a function with a personality.", &CPI);
3731 // The cleanuppad instruction must be the first non-PHI instruction in the
3732 // block.
3733 Assert(BB->getFirstNonPHI() == &CPI,
3734 "CleanupPadInst not the first non-PHI instruction in the block.",
3735 &CPI);
3737 auto *ParentPad = CPI.getParentPad();
3738 Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
3739 "CleanupPadInst has an invalid parent.", &CPI);
3741 visitEHPadPredecessors(CPI);
3742 visitFuncletPadInst(CPI);
3745 void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
3746 User *FirstUser = nullptr;
3747 Value *FirstUnwindPad = nullptr;
3748 SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
3749 SmallSet<FuncletPadInst *, 8> Seen;
3751 while (!Worklist.empty()) {
3752 FuncletPadInst *CurrentPad = Worklist.pop_back_val();
3753 Assert(Seen.insert(CurrentPad).second,
3754 "FuncletPadInst must not be nested within itself", CurrentPad);
3755 Value *UnresolvedAncestorPad = nullptr;
3756 for (User *U : CurrentPad->users()) {
3757 BasicBlock *UnwindDest;
3758 if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
3759 UnwindDest = CRI->getUnwindDest();
3760 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
3761 // We allow catchswitch unwind to caller to nest
3762 // within an outer pad that unwinds somewhere else,
3763 // because catchswitch doesn't have a nounwind variant.
3764 // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
3765 if (CSI->unwindsToCaller())
3766 continue;
3767 UnwindDest = CSI->getUnwindDest();
3768 } else if (auto *II = dyn_cast<InvokeInst>(U)) {
3769 UnwindDest = II->getUnwindDest();
3770 } else if (isa<CallInst>(U)) {
3771 // Calls which don't unwind may be found inside funclet
3772 // pads that unwind somewhere else. We don't *require*
3773 // such calls to be annotated nounwind.
3774 continue;
3775 } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
3776 // The unwind dest for a cleanup can only be found by
3777 // recursive search. Add it to the worklist, and we'll
3778 // search for its first use that determines where it unwinds.
3779 Worklist.push_back(CPI);
3780 continue;
3781 } else {
3782 Assert(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
3783 continue;
3786 Value *UnwindPad;
3787 bool ExitsFPI;
3788 if (UnwindDest) {
3789 UnwindPad = UnwindDest->getFirstNonPHI();
3790 if (!cast<Instruction>(UnwindPad)->isEHPad())
3791 continue;
3792 Value *UnwindParent = getParentPad(UnwindPad);
3793 // Ignore unwind edges that don't exit CurrentPad.
3794 if (UnwindParent == CurrentPad)
3795 continue;
3796 // Determine whether the original funclet pad is exited,
3797 // and if we are scanning nested pads determine how many
3798 // of them are exited so we can stop searching their
3799 // children.
3800 Value *ExitedPad = CurrentPad;
3801 ExitsFPI = false;
3802 do {
3803 if (ExitedPad == &FPI) {
3804 ExitsFPI = true;
3805 // Now we can resolve any ancestors of CurrentPad up to
3806 // FPI, but not including FPI since we need to make sure
3807 // to check all direct users of FPI for consistency.
3808 UnresolvedAncestorPad = &FPI;
3809 break;
3811 Value *ExitedParent = getParentPad(ExitedPad);
3812 if (ExitedParent == UnwindParent) {
3813 // ExitedPad is the ancestor-most pad which this unwind
3814 // edge exits, so we can resolve up to it, meaning that
3815 // ExitedParent is the first ancestor still unresolved.
3816 UnresolvedAncestorPad = ExitedParent;
3817 break;
3819 ExitedPad = ExitedParent;
3820 } while (!isa<ConstantTokenNone>(ExitedPad));
3821 } else {
3822 // Unwinding to caller exits all pads.
3823 UnwindPad = ConstantTokenNone::get(FPI.getContext());
3824 ExitsFPI = true;
3825 UnresolvedAncestorPad = &FPI;
3828 if (ExitsFPI) {
3829 // This unwind edge exits FPI. Make sure it agrees with other
3830 // such edges.
3831 if (FirstUser) {
3832 Assert(UnwindPad == FirstUnwindPad, "Unwind edges out of a funclet "
3833 "pad must have the same unwind "
3834 "dest",
3835 &FPI, U, FirstUser);
3836 } else {
3837 FirstUser = U;
3838 FirstUnwindPad = UnwindPad;
3839 // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
3840 if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
3841 getParentPad(UnwindPad) == getParentPad(&FPI))
3842 SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
3845 // Make sure we visit all uses of FPI, but for nested pads stop as
3846 // soon as we know where they unwind to.
3847 if (CurrentPad != &FPI)
3848 break;
3850 if (UnresolvedAncestorPad) {
3851 if (CurrentPad == UnresolvedAncestorPad) {
3852 // When CurrentPad is FPI itself, we don't mark it as resolved even if
3853 // we've found an unwind edge that exits it, because we need to verify
3854 // all direct uses of FPI.
3855 assert(CurrentPad == &FPI);
3856 continue;
3858 // Pop off the worklist any nested pads that we've found an unwind
3859 // destination for. The pads on the worklist are the uncles,
3860 // great-uncles, etc. of CurrentPad. We've found an unwind destination
3861 // for all ancestors of CurrentPad up to but not including
3862 // UnresolvedAncestorPad.
3863 Value *ResolvedPad = CurrentPad;
3864 while (!Worklist.empty()) {
3865 Value *UnclePad = Worklist.back();
3866 Value *AncestorPad = getParentPad(UnclePad);
3867 // Walk ResolvedPad up the ancestor list until we either find the
3868 // uncle's parent or the last resolved ancestor.
3869 while (ResolvedPad != AncestorPad) {
3870 Value *ResolvedParent = getParentPad(ResolvedPad);
3871 if (ResolvedParent == UnresolvedAncestorPad) {
3872 break;
3874 ResolvedPad = ResolvedParent;
3876 // If the resolved ancestor search didn't find the uncle's parent,
3877 // then the uncle is not yet resolved.
3878 if (ResolvedPad != AncestorPad)
3879 break;
3880 // This uncle is resolved, so pop it from the worklist.
3881 Worklist.pop_back();
3886 if (FirstUnwindPad) {
3887 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
3888 BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
3889 Value *SwitchUnwindPad;
3890 if (SwitchUnwindDest)
3891 SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
3892 else
3893 SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
3894 Assert(SwitchUnwindPad == FirstUnwindPad,
3895 "Unwind edges out of a catch must have the same unwind dest as "
3896 "the parent catchswitch",
3897 &FPI, FirstUser, CatchSwitch);
3901 visitInstruction(FPI);
3904 void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
3905 BasicBlock *BB = CatchSwitch.getParent();
3907 Function *F = BB->getParent();
3908 Assert(F->hasPersonalityFn(),
3909 "CatchSwitchInst needs to be in a function with a personality.",
3910 &CatchSwitch);
3912 // The catchswitch instruction must be the first non-PHI instruction in the
3913 // block.
3914 Assert(BB->getFirstNonPHI() == &CatchSwitch,
3915 "CatchSwitchInst not the first non-PHI instruction in the block.",
3916 &CatchSwitch);
3918 auto *ParentPad = CatchSwitch.getParentPad();
3919 Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
3920 "CatchSwitchInst has an invalid parent.", ParentPad);
3922 if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
3923 Instruction *I = UnwindDest->getFirstNonPHI();
3924 Assert(I->isEHPad() && !isa<LandingPadInst>(I),
3925 "CatchSwitchInst must unwind to an EH block which is not a "
3926 "landingpad.",
3927 &CatchSwitch);
3929 // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
3930 if (getParentPad(I) == ParentPad)
3931 SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
3934 Assert(CatchSwitch.getNumHandlers() != 0,
3935 "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
3937 for (BasicBlock *Handler : CatchSwitch.handlers()) {
3938 Assert(isa<CatchPadInst>(Handler->getFirstNonPHI()),
3939 "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler);
3942 visitEHPadPredecessors(CatchSwitch);
3943 visitTerminator(CatchSwitch);
3946 void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
3947 Assert(isa<CleanupPadInst>(CRI.getOperand(0)),
3948 "CleanupReturnInst needs to be provided a CleanupPad", &CRI,
3949 CRI.getOperand(0));
3951 if (BasicBlock *UnwindDest = CRI.getUnwindDest()) {
3952 Instruction *I = UnwindDest->getFirstNonPHI();
3953 Assert(I->isEHPad() && !isa<LandingPadInst>(I),
3954 "CleanupReturnInst must unwind to an EH block which is not a "
3955 "landingpad.",
3956 &CRI);
3959 visitTerminator(CRI);
3962 void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
3963 Instruction *Op = cast<Instruction>(I.getOperand(i));
3964 // If the we have an invalid invoke, don't try to compute the dominance.
3965 // We already reject it in the invoke specific checks and the dominance
3966 // computation doesn't handle multiple edges.
3967 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
3968 if (II->getNormalDest() == II->getUnwindDest())
3969 return;
3972 // Quick check whether the def has already been encountered in the same block.
3973 // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
3974 // uses are defined to happen on the incoming edge, not at the instruction.
3976 // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
3977 // wrapping an SSA value, assert that we've already encountered it. See
3978 // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
3979 if (!isa<PHINode>(I) && InstsInThisBlock.count(Op))
3980 return;
3982 const Use &U = I.getOperandUse(i);
3983 Assert(DT.dominates(Op, U),
3984 "Instruction does not dominate all uses!", Op, &I);
3987 void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
3988 Assert(I.getType()->isPointerTy(), "dereferenceable, dereferenceable_or_null "
3989 "apply only to pointer types", &I);
3990 Assert((isa<LoadInst>(I) || isa<IntToPtrInst>(I)),
3991 "dereferenceable, dereferenceable_or_null apply only to load"
3992 " and inttoptr instructions, use attributes for calls or invokes", &I);
3993 Assert(MD->getNumOperands() == 1, "dereferenceable, dereferenceable_or_null "
3994 "take one operand!", &I);
3995 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0));
3996 Assert(CI && CI->getType()->isIntegerTy(64), "dereferenceable, "
3997 "dereferenceable_or_null metadata value must be an i64!", &I);
4000 void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
4001 Assert(MD->getNumOperands() >= 2,
4002 "!prof annotations should have no less than 2 operands", MD);
4004 // Check first operand.
4005 Assert(MD->getOperand(0) != nullptr, "first operand should not be null", MD);
4006 Assert(isa<MDString>(MD->getOperand(0)),
4007 "expected string with name of the !prof annotation", MD);
4008 MDString *MDS = cast<MDString>(MD->getOperand(0));
4009 StringRef ProfName = MDS->getString();
4011 // Check consistency of !prof branch_weights metadata.
4012 if (ProfName.equals("branch_weights")) {
4013 unsigned ExpectedNumOperands = 0;
4014 if (BranchInst *BI = dyn_cast<BranchInst>(&I))
4015 ExpectedNumOperands = BI->getNumSuccessors();
4016 else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
4017 ExpectedNumOperands = SI->getNumSuccessors();
4018 else if (isa<CallInst>(&I) || isa<InvokeInst>(&I))
4019 ExpectedNumOperands = 1;
4020 else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
4021 ExpectedNumOperands = IBI->getNumDestinations();
4022 else if (isa<SelectInst>(&I))
4023 ExpectedNumOperands = 2;
4024 else
4025 CheckFailed("!prof branch_weights are not allowed for this instruction",
4026 MD);
4028 Assert(MD->getNumOperands() == 1 + ExpectedNumOperands,
4029 "Wrong number of operands", MD);
4030 for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
4031 auto &MDO = MD->getOperand(i);
4032 Assert(MDO, "second operand should not be null", MD);
4033 Assert(mdconst::dyn_extract<ConstantInt>(MDO),
4034 "!prof brunch_weights operand is not a const int");
4039 /// verifyInstruction - Verify that an instruction is well formed.
4041 void Verifier::visitInstruction(Instruction &I) {
4042 BasicBlock *BB = I.getParent();
4043 Assert(BB, "Instruction not embedded in basic block!", &I);
4045 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
4046 for (User *U : I.users()) {
4047 Assert(U != (User *)&I || !DT.isReachableFromEntry(BB),
4048 "Only PHI nodes may reference their own value!", &I);
4052 // Check that void typed values don't have names
4053 Assert(!I.getType()->isVoidTy() || !I.hasName(),
4054 "Instruction has a name, but provides a void value!", &I);
4056 // Check that the return value of the instruction is either void or a legal
4057 // value type.
4058 Assert(I.getType()->isVoidTy() || I.getType()->isFirstClassType(),
4059 "Instruction returns a non-scalar type!", &I);
4061 // Check that the instruction doesn't produce metadata. Calls are already
4062 // checked against the callee type.
4063 Assert(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I),
4064 "Invalid use of metadata!", &I);
4066 // Check that all uses of the instruction, if they are instructions
4067 // themselves, actually have parent basic blocks. If the use is not an
4068 // instruction, it is an error!
4069 for (Use &U : I.uses()) {
4070 if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
4071 Assert(Used->getParent() != nullptr,
4072 "Instruction referencing"
4073 " instruction not embedded in a basic block!",
4074 &I, Used);
4075 else {
4076 CheckFailed("Use of instruction is not an instruction!", U);
4077 return;
4081 // Get a pointer to the call base of the instruction if it is some form of
4082 // call.
4083 const CallBase *CBI = dyn_cast<CallBase>(&I);
4085 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
4086 Assert(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
4088 // Check to make sure that only first-class-values are operands to
4089 // instructions.
4090 if (!I.getOperand(i)->getType()->isFirstClassType()) {
4091 Assert(false, "Instruction operands must be first-class values!", &I);
4094 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
4095 // Check to make sure that the "address of" an intrinsic function is never
4096 // taken.
4097 Assert(!F->isIntrinsic() ||
4098 (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)),
4099 "Cannot take the address of an intrinsic!", &I);
4100 Assert(
4101 !F->isIntrinsic() || isa<CallInst>(I) ||
4102 F->getIntrinsicID() == Intrinsic::donothing ||
4103 F->getIntrinsicID() == Intrinsic::coro_resume ||
4104 F->getIntrinsicID() == Intrinsic::coro_destroy ||
4105 F->getIntrinsicID() == Intrinsic::experimental_patchpoint_void ||
4106 F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
4107 F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
4108 F->getIntrinsicID() == Intrinsic::wasm_rethrow_in_catch,
4109 "Cannot invoke an intrinsic other than donothing, patchpoint, "
4110 "statepoint, coro_resume or coro_destroy",
4111 &I);
4112 Assert(F->getParent() == &M, "Referencing function in another module!",
4113 &I, &M, F, F->getParent());
4114 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
4115 Assert(OpBB->getParent() == BB->getParent(),
4116 "Referring to a basic block in another function!", &I);
4117 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
4118 Assert(OpArg->getParent() == BB->getParent(),
4119 "Referring to an argument in another function!", &I);
4120 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
4121 Assert(GV->getParent() == &M, "Referencing global in another module!", &I,
4122 &M, GV, GV->getParent());
4123 } else if (isa<Instruction>(I.getOperand(i))) {
4124 verifyDominatesUse(I, i);
4125 } else if (isa<InlineAsm>(I.getOperand(i))) {
4126 Assert(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
4127 "Cannot take the address of an inline asm!", &I);
4128 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
4129 if (CE->getType()->isPtrOrPtrVectorTy() ||
4130 !DL.getNonIntegralAddressSpaces().empty()) {
4131 // If we have a ConstantExpr pointer, we need to see if it came from an
4132 // illegal bitcast. If the datalayout string specifies non-integral
4133 // address spaces then we also need to check for illegal ptrtoint and
4134 // inttoptr expressions.
4135 visitConstantExprsRecursively(CE);
4140 if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
4141 Assert(I.getType()->isFPOrFPVectorTy(),
4142 "fpmath requires a floating point result!", &I);
4143 Assert(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
4144 if (ConstantFP *CFP0 =
4145 mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
4146 const APFloat &Accuracy = CFP0->getValueAPF();
4147 Assert(&Accuracy.getSemantics() == &APFloat::IEEEsingle(),
4148 "fpmath accuracy must have float type", &I);
4149 Assert(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
4150 "fpmath accuracy not a positive number!", &I);
4151 } else {
4152 Assert(false, "invalid fpmath accuracy!", &I);
4156 if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
4157 Assert(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
4158 "Ranges are only for loads, calls and invokes!", &I);
4159 visitRangeMetadata(I, Range, I.getType());
4162 if (I.getMetadata(LLVMContext::MD_nonnull)) {
4163 Assert(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
4164 &I);
4165 Assert(isa<LoadInst>(I),
4166 "nonnull applies only to load instructions, use attributes"
4167 " for calls or invokes",
4168 &I);
4171 if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
4172 visitDereferenceableMetadata(I, MD);
4174 if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
4175 visitDereferenceableMetadata(I, MD);
4177 if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa))
4178 TBAAVerifyHelper.visitTBAAMetadata(I, TBAA);
4180 if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
4181 Assert(I.getType()->isPointerTy(), "align applies only to pointer types",
4182 &I);
4183 Assert(isa<LoadInst>(I), "align applies only to load instructions, "
4184 "use attributes for calls or invokes", &I);
4185 Assert(AlignMD->getNumOperands() == 1, "align takes one operand!", &I);
4186 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0));
4187 Assert(CI && CI->getType()->isIntegerTy(64),
4188 "align metadata value must be an i64!", &I);
4189 uint64_t Align = CI->getZExtValue();
4190 Assert(isPowerOf2_64(Align),
4191 "align metadata value must be a power of 2!", &I);
4192 Assert(Align <= Value::MaximumAlignment,
4193 "alignment is larger that implementation defined limit", &I);
4196 if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof))
4197 visitProfMetadata(I, MD);
4199 if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
4200 AssertDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
4201 visitMDNode(*N);
4204 if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I))
4205 verifyFragmentExpression(*DII);
4207 InstsInThisBlock.insert(&I);
4210 /// Allow intrinsics to be verified in different ways.
4211 void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
4212 Function *IF = Call.getCalledFunction();
4213 Assert(IF->isDeclaration(), "Intrinsic functions should never be defined!",
4214 IF);
4216 // Verify that the intrinsic prototype lines up with what the .td files
4217 // describe.
4218 FunctionType *IFTy = IF->getFunctionType();
4219 bool IsVarArg = IFTy->isVarArg();
4221 SmallVector<Intrinsic::IITDescriptor, 8> Table;
4222 getIntrinsicInfoTableEntries(ID, Table);
4223 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
4225 // Walk the descriptors to extract overloaded types.
4226 SmallVector<Type *, 4> ArgTys;
4227 Intrinsic::MatchIntrinsicTypesResult Res =
4228 Intrinsic::matchIntrinsicSignature(IFTy, TableRef, ArgTys);
4229 Assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet,
4230 "Intrinsic has incorrect return type!", IF);
4231 Assert(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg,
4232 "Intrinsic has incorrect argument type!", IF);
4234 // Verify if the intrinsic call matches the vararg property.
4235 if (IsVarArg)
4236 Assert(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
4237 "Intrinsic was not defined with variable arguments!", IF);
4238 else
4239 Assert(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
4240 "Callsite was not defined with variable arguments!", IF);
4242 // All descriptors should be absorbed by now.
4243 Assert(TableRef.empty(), "Intrinsic has too few arguments!", IF);
4245 // Now that we have the intrinsic ID and the actual argument types (and we
4246 // know they are legal for the intrinsic!) get the intrinsic name through the
4247 // usual means. This allows us to verify the mangling of argument types into
4248 // the name.
4249 const std::string ExpectedName = Intrinsic::getName(ID, ArgTys);
4250 Assert(ExpectedName == IF->getName(),
4251 "Intrinsic name not mangled correctly for type arguments! "
4252 "Should be: " +
4253 ExpectedName,
4254 IF);
4256 // If the intrinsic takes MDNode arguments, verify that they are either global
4257 // or are local to *this* function.
4258 for (Value *V : Call.args())
4259 if (auto *MD = dyn_cast<MetadataAsValue>(V))
4260 visitMetadataAsValue(*MD, Call.getCaller());
4262 switch (ID) {
4263 default:
4264 break;
4265 case Intrinsic::coro_id: {
4266 auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
4267 if (isa<ConstantPointerNull>(InfoArg))
4268 break;
4269 auto *GV = dyn_cast<GlobalVariable>(InfoArg);
4270 Assert(GV && GV->isConstant() && GV->hasDefinitiveInitializer(),
4271 "info argument of llvm.coro.begin must refer to an initialized "
4272 "constant");
4273 Constant *Init = GV->getInitializer();
4274 Assert(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init),
4275 "info argument of llvm.coro.begin must refer to either a struct or "
4276 "an array");
4277 break;
4279 case Intrinsic::experimental_constrained_fadd:
4280 case Intrinsic::experimental_constrained_fsub:
4281 case Intrinsic::experimental_constrained_fmul:
4282 case Intrinsic::experimental_constrained_fdiv:
4283 case Intrinsic::experimental_constrained_frem:
4284 case Intrinsic::experimental_constrained_fma:
4285 case Intrinsic::experimental_constrained_fptrunc:
4286 case Intrinsic::experimental_constrained_fpext:
4287 case Intrinsic::experimental_constrained_sqrt:
4288 case Intrinsic::experimental_constrained_pow:
4289 case Intrinsic::experimental_constrained_powi:
4290 case Intrinsic::experimental_constrained_sin:
4291 case Intrinsic::experimental_constrained_cos:
4292 case Intrinsic::experimental_constrained_exp:
4293 case Intrinsic::experimental_constrained_exp2:
4294 case Intrinsic::experimental_constrained_log:
4295 case Intrinsic::experimental_constrained_log10:
4296 case Intrinsic::experimental_constrained_log2:
4297 case Intrinsic::experimental_constrained_rint:
4298 case Intrinsic::experimental_constrained_nearbyint:
4299 case Intrinsic::experimental_constrained_maxnum:
4300 case Intrinsic::experimental_constrained_minnum:
4301 case Intrinsic::experimental_constrained_ceil:
4302 case Intrinsic::experimental_constrained_floor:
4303 case Intrinsic::experimental_constrained_round:
4304 case Intrinsic::experimental_constrained_trunc:
4305 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call));
4306 break;
4307 case Intrinsic::dbg_declare: // llvm.dbg.declare
4308 Assert(isa<MetadataAsValue>(Call.getArgOperand(0)),
4309 "invalid llvm.dbg.declare intrinsic call 1", Call);
4310 visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call));
4311 break;
4312 case Intrinsic::dbg_addr: // llvm.dbg.addr
4313 visitDbgIntrinsic("addr", cast<DbgVariableIntrinsic>(Call));
4314 break;
4315 case Intrinsic::dbg_value: // llvm.dbg.value
4316 visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call));
4317 break;
4318 case Intrinsic::dbg_label: // llvm.dbg.label
4319 visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call));
4320 break;
4321 case Intrinsic::memcpy:
4322 case Intrinsic::memmove:
4323 case Intrinsic::memset: {
4324 const auto *MI = cast<MemIntrinsic>(&Call);
4325 auto IsValidAlignment = [&](unsigned Alignment) -> bool {
4326 return Alignment == 0 || isPowerOf2_32(Alignment);
4328 Assert(IsValidAlignment(MI->getDestAlignment()),
4329 "alignment of arg 0 of memory intrinsic must be 0 or a power of 2",
4330 Call);
4331 if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
4332 Assert(IsValidAlignment(MTI->getSourceAlignment()),
4333 "alignment of arg 1 of memory intrinsic must be 0 or a power of 2",
4334 Call);
4337 break;
4339 case Intrinsic::memcpy_element_unordered_atomic:
4340 case Intrinsic::memmove_element_unordered_atomic:
4341 case Intrinsic::memset_element_unordered_atomic: {
4342 const auto *AMI = cast<AtomicMemIntrinsic>(&Call);
4344 ConstantInt *ElementSizeCI =
4345 cast<ConstantInt>(AMI->getRawElementSizeInBytes());
4346 const APInt &ElementSizeVal = ElementSizeCI->getValue();
4347 Assert(ElementSizeVal.isPowerOf2(),
4348 "element size of the element-wise atomic memory intrinsic "
4349 "must be a power of 2",
4350 Call);
4352 if (auto *LengthCI = dyn_cast<ConstantInt>(AMI->getLength())) {
4353 uint64_t Length = LengthCI->getZExtValue();
4354 uint64_t ElementSize = AMI->getElementSizeInBytes();
4355 Assert((Length % ElementSize) == 0,
4356 "constant length must be a multiple of the element size in the "
4357 "element-wise atomic memory intrinsic",
4358 Call);
4361 auto IsValidAlignment = [&](uint64_t Alignment) {
4362 return isPowerOf2_64(Alignment) && ElementSizeVal.ule(Alignment);
4364 uint64_t DstAlignment = AMI->getDestAlignment();
4365 Assert(IsValidAlignment(DstAlignment),
4366 "incorrect alignment of the destination argument", Call);
4367 if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) {
4368 uint64_t SrcAlignment = AMT->getSourceAlignment();
4369 Assert(IsValidAlignment(SrcAlignment),
4370 "incorrect alignment of the source argument", Call);
4372 break;
4374 case Intrinsic::gcroot:
4375 case Intrinsic::gcwrite:
4376 case Intrinsic::gcread:
4377 if (ID == Intrinsic::gcroot) {
4378 AllocaInst *AI =
4379 dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts());
4380 Assert(AI, "llvm.gcroot parameter #1 must be an alloca.", Call);
4381 Assert(isa<Constant>(Call.getArgOperand(1)),
4382 "llvm.gcroot parameter #2 must be a constant.", Call);
4383 if (!AI->getAllocatedType()->isPointerTy()) {
4384 Assert(!isa<ConstantPointerNull>(Call.getArgOperand(1)),
4385 "llvm.gcroot parameter #1 must either be a pointer alloca, "
4386 "or argument #2 must be a non-null constant.",
4387 Call);
4391 Assert(Call.getParent()->getParent()->hasGC(),
4392 "Enclosing function does not use GC.", Call);
4393 break;
4394 case Intrinsic::init_trampoline:
4395 Assert(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()),
4396 "llvm.init_trampoline parameter #2 must resolve to a function.",
4397 Call);
4398 break;
4399 case Intrinsic::prefetch:
4400 Assert(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2 &&
4401 cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
4402 "invalid arguments to llvm.prefetch", Call);
4403 break;
4404 case Intrinsic::stackprotector:
4405 Assert(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()),
4406 "llvm.stackprotector parameter #2 must resolve to an alloca.", Call);
4407 break;
4408 case Intrinsic::localescape: {
4409 BasicBlock *BB = Call.getParent();
4410 Assert(BB == &BB->getParent()->front(),
4411 "llvm.localescape used outside of entry block", Call);
4412 Assert(!SawFrameEscape,
4413 "multiple calls to llvm.localescape in one function", Call);
4414 for (Value *Arg : Call.args()) {
4415 if (isa<ConstantPointerNull>(Arg))
4416 continue; // Null values are allowed as placeholders.
4417 auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
4418 Assert(AI && AI->isStaticAlloca(),
4419 "llvm.localescape only accepts static allocas", Call);
4421 FrameEscapeInfo[BB->getParent()].first = Call.getNumArgOperands();
4422 SawFrameEscape = true;
4423 break;
4425 case Intrinsic::localrecover: {
4426 Value *FnArg = Call.getArgOperand(0)->stripPointerCasts();
4427 Function *Fn = dyn_cast<Function>(FnArg);
4428 Assert(Fn && !Fn->isDeclaration(),
4429 "llvm.localrecover first "
4430 "argument must be function defined in this module",
4431 Call);
4432 auto *IdxArg = cast<ConstantInt>(Call.getArgOperand(2));
4433 auto &Entry = FrameEscapeInfo[Fn];
4434 Entry.second = unsigned(
4435 std::max(uint64_t(Entry.second), IdxArg->getLimitedValue(~0U) + 1));
4436 break;
4439 case Intrinsic::experimental_gc_statepoint:
4440 if (auto *CI = dyn_cast<CallInst>(&Call))
4441 Assert(!CI->isInlineAsm(),
4442 "gc.statepoint support for inline assembly unimplemented", CI);
4443 Assert(Call.getParent()->getParent()->hasGC(),
4444 "Enclosing function does not use GC.", Call);
4446 verifyStatepoint(Call);
4447 break;
4448 case Intrinsic::experimental_gc_result: {
4449 Assert(Call.getParent()->getParent()->hasGC(),
4450 "Enclosing function does not use GC.", Call);
4451 // Are we tied to a statepoint properly?
4452 const auto *StatepointCall = dyn_cast<CallBase>(Call.getArgOperand(0));
4453 const Function *StatepointFn =
4454 StatepointCall ? StatepointCall->getCalledFunction() : nullptr;
4455 Assert(StatepointFn && StatepointFn->isDeclaration() &&
4456 StatepointFn->getIntrinsicID() ==
4457 Intrinsic::experimental_gc_statepoint,
4458 "gc.result operand #1 must be from a statepoint", Call,
4459 Call.getArgOperand(0));
4461 // Assert that result type matches wrapped callee.
4462 const Value *Target = StatepointCall->getArgOperand(2);
4463 auto *PT = cast<PointerType>(Target->getType());
4464 auto *TargetFuncType = cast<FunctionType>(PT->getElementType());
4465 Assert(Call.getType() == TargetFuncType->getReturnType(),
4466 "gc.result result type does not match wrapped callee", Call);
4467 break;
4469 case Intrinsic::experimental_gc_relocate: {
4470 Assert(Call.getNumArgOperands() == 3, "wrong number of arguments", Call);
4472 Assert(isa<PointerType>(Call.getType()->getScalarType()),
4473 "gc.relocate must return a pointer or a vector of pointers", Call);
4475 // Check that this relocate is correctly tied to the statepoint
4477 // This is case for relocate on the unwinding path of an invoke statepoint
4478 if (LandingPadInst *LandingPad =
4479 dyn_cast<LandingPadInst>(Call.getArgOperand(0))) {
4481 const BasicBlock *InvokeBB =
4482 LandingPad->getParent()->getUniquePredecessor();
4484 // Landingpad relocates should have only one predecessor with invoke
4485 // statepoint terminator
4486 Assert(InvokeBB, "safepoints should have unique landingpads",
4487 LandingPad->getParent());
4488 Assert(InvokeBB->getTerminator(), "safepoint block should be well formed",
4489 InvokeBB);
4490 Assert(isStatepoint(InvokeBB->getTerminator()),
4491 "gc relocate should be linked to a statepoint", InvokeBB);
4492 } else {
4493 // In all other cases relocate should be tied to the statepoint directly.
4494 // This covers relocates on a normal return path of invoke statepoint and
4495 // relocates of a call statepoint.
4496 auto Token = Call.getArgOperand(0);
4497 Assert(isa<Instruction>(Token) && isStatepoint(cast<Instruction>(Token)),
4498 "gc relocate is incorrectly tied to the statepoint", Call, Token);
4501 // Verify rest of the relocate arguments.
4502 const CallBase &StatepointCall =
4503 *cast<CallBase>(cast<GCRelocateInst>(Call).getStatepoint());
4505 // Both the base and derived must be piped through the safepoint.
4506 Value *Base = Call.getArgOperand(1);
4507 Assert(isa<ConstantInt>(Base),
4508 "gc.relocate operand #2 must be integer offset", Call);
4510 Value *Derived = Call.getArgOperand(2);
4511 Assert(isa<ConstantInt>(Derived),
4512 "gc.relocate operand #3 must be integer offset", Call);
4514 const int BaseIndex = cast<ConstantInt>(Base)->getZExtValue();
4515 const int DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue();
4516 // Check the bounds
4517 Assert(0 <= BaseIndex && BaseIndex < (int)StatepointCall.arg_size(),
4518 "gc.relocate: statepoint base index out of bounds", Call);
4519 Assert(0 <= DerivedIndex && DerivedIndex < (int)StatepointCall.arg_size(),
4520 "gc.relocate: statepoint derived index out of bounds", Call);
4522 // Check that BaseIndex and DerivedIndex fall within the 'gc parameters'
4523 // section of the statepoint's argument.
4524 Assert(StatepointCall.arg_size() > 0,
4525 "gc.statepoint: insufficient arguments");
4526 Assert(isa<ConstantInt>(StatepointCall.getArgOperand(3)),
4527 "gc.statement: number of call arguments must be constant integer");
4528 const unsigned NumCallArgs =
4529 cast<ConstantInt>(StatepointCall.getArgOperand(3))->getZExtValue();
4530 Assert(StatepointCall.arg_size() > NumCallArgs + 5,
4531 "gc.statepoint: mismatch in number of call arguments");
4532 Assert(isa<ConstantInt>(StatepointCall.getArgOperand(NumCallArgs + 5)),
4533 "gc.statepoint: number of transition arguments must be "
4534 "a constant integer");
4535 const int NumTransitionArgs =
4536 cast<ConstantInt>(StatepointCall.getArgOperand(NumCallArgs + 5))
4537 ->getZExtValue();
4538 const int DeoptArgsStart = 4 + NumCallArgs + 1 + NumTransitionArgs + 1;
4539 Assert(isa<ConstantInt>(StatepointCall.getArgOperand(DeoptArgsStart)),
4540 "gc.statepoint: number of deoptimization arguments must be "
4541 "a constant integer");
4542 const int NumDeoptArgs =
4543 cast<ConstantInt>(StatepointCall.getArgOperand(DeoptArgsStart))
4544 ->getZExtValue();
4545 const int GCParamArgsStart = DeoptArgsStart + 1 + NumDeoptArgs;
4546 const int GCParamArgsEnd = StatepointCall.arg_size();
4547 Assert(GCParamArgsStart <= BaseIndex && BaseIndex < GCParamArgsEnd,
4548 "gc.relocate: statepoint base index doesn't fall within the "
4549 "'gc parameters' section of the statepoint call",
4550 Call);
4551 Assert(GCParamArgsStart <= DerivedIndex && DerivedIndex < GCParamArgsEnd,
4552 "gc.relocate: statepoint derived index doesn't fall within the "
4553 "'gc parameters' section of the statepoint call",
4554 Call);
4556 // Relocated value must be either a pointer type or vector-of-pointer type,
4557 // but gc_relocate does not need to return the same pointer type as the
4558 // relocated pointer. It can be casted to the correct type later if it's
4559 // desired. However, they must have the same address space and 'vectorness'
4560 GCRelocateInst &Relocate = cast<GCRelocateInst>(Call);
4561 Assert(Relocate.getDerivedPtr()->getType()->isPtrOrPtrVectorTy(),
4562 "gc.relocate: relocated value must be a gc pointer", Call);
4564 auto ResultType = Call.getType();
4565 auto DerivedType = Relocate.getDerivedPtr()->getType();
4566 Assert(ResultType->isVectorTy() == DerivedType->isVectorTy(),
4567 "gc.relocate: vector relocates to vector and pointer to pointer",
4568 Call);
4569 Assert(
4570 ResultType->getPointerAddressSpace() ==
4571 DerivedType->getPointerAddressSpace(),
4572 "gc.relocate: relocating a pointer shouldn't change its address space",
4573 Call);
4574 break;
4576 case Intrinsic::eh_exceptioncode:
4577 case Intrinsic::eh_exceptionpointer: {
4578 Assert(isa<CatchPadInst>(Call.getArgOperand(0)),
4579 "eh.exceptionpointer argument must be a catchpad", Call);
4580 break;
4582 case Intrinsic::masked_load: {
4583 Assert(Call.getType()->isVectorTy(), "masked_load: must return a vector",
4584 Call);
4586 Value *Ptr = Call.getArgOperand(0);
4587 ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(1));
4588 Value *Mask = Call.getArgOperand(2);
4589 Value *PassThru = Call.getArgOperand(3);
4590 Assert(Mask->getType()->isVectorTy(), "masked_load: mask must be vector",
4591 Call);
4592 Assert(Alignment->getValue().isPowerOf2(),
4593 "masked_load: alignment must be a power of 2", Call);
4595 // DataTy is the overloaded type
4596 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
4597 Assert(DataTy == Call.getType(),
4598 "masked_load: return must match pointer type", Call);
4599 Assert(PassThru->getType() == DataTy,
4600 "masked_load: pass through and data type must match", Call);
4601 Assert(Mask->getType()->getVectorNumElements() ==
4602 DataTy->getVectorNumElements(),
4603 "masked_load: vector mask must be same length as data", Call);
4604 break;
4606 case Intrinsic::masked_store: {
4607 Value *Val = Call.getArgOperand(0);
4608 Value *Ptr = Call.getArgOperand(1);
4609 ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(2));
4610 Value *Mask = Call.getArgOperand(3);
4611 Assert(Mask->getType()->isVectorTy(), "masked_store: mask must be vector",
4612 Call);
4613 Assert(Alignment->getValue().isPowerOf2(),
4614 "masked_store: alignment must be a power of 2", Call);
4616 // DataTy is the overloaded type
4617 Type *DataTy = cast<PointerType>(Ptr->getType())->getElementType();
4618 Assert(DataTy == Val->getType(),
4619 "masked_store: storee must match pointer type", Call);
4620 Assert(Mask->getType()->getVectorNumElements() ==
4621 DataTy->getVectorNumElements(),
4622 "masked_store: vector mask must be same length as data", Call);
4623 break;
4626 case Intrinsic::experimental_guard: {
4627 Assert(isa<CallInst>(Call), "experimental_guard cannot be invoked", Call);
4628 Assert(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
4629 "experimental_guard must have exactly one "
4630 "\"deopt\" operand bundle");
4631 break;
4634 case Intrinsic::experimental_deoptimize: {
4635 Assert(isa<CallInst>(Call), "experimental_deoptimize cannot be invoked",
4636 Call);
4637 Assert(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
4638 "experimental_deoptimize must have exactly one "
4639 "\"deopt\" operand bundle");
4640 Assert(Call.getType() == Call.getFunction()->getReturnType(),
4641 "experimental_deoptimize return type must match caller return type");
4643 if (isa<CallInst>(Call)) {
4644 auto *RI = dyn_cast<ReturnInst>(Call.getNextNode());
4645 Assert(RI,
4646 "calls to experimental_deoptimize must be followed by a return");
4648 if (!Call.getType()->isVoidTy() && RI)
4649 Assert(RI->getReturnValue() == &Call,
4650 "calls to experimental_deoptimize must be followed by a return "
4651 "of the value computed by experimental_deoptimize");
4654 break;
4656 case Intrinsic::sadd_sat:
4657 case Intrinsic::uadd_sat:
4658 case Intrinsic::ssub_sat:
4659 case Intrinsic::usub_sat: {
4660 Value *Op1 = Call.getArgOperand(0);
4661 Value *Op2 = Call.getArgOperand(1);
4662 Assert(Op1->getType()->isIntOrIntVectorTy(),
4663 "first operand of [us][add|sub]_sat must be an int type or vector "
4664 "of ints");
4665 Assert(Op2->getType()->isIntOrIntVectorTy(),
4666 "second operand of [us][add|sub]_sat must be an int type or vector "
4667 "of ints");
4668 break;
4670 case Intrinsic::smul_fix:
4671 case Intrinsic::smul_fix_sat:
4672 case Intrinsic::umul_fix: {
4673 Value *Op1 = Call.getArgOperand(0);
4674 Value *Op2 = Call.getArgOperand(1);
4675 Assert(Op1->getType()->isIntOrIntVectorTy(),
4676 "first operand of [us]mul_fix[_sat] must be an int type or vector "
4677 "of ints");
4678 Assert(Op2->getType()->isIntOrIntVectorTy(),
4679 "second operand of [us]mul_fix_[sat] must be an int type or vector "
4680 "of ints");
4682 auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2));
4683 Assert(Op3->getType()->getBitWidth() <= 32,
4684 "third argument of [us]mul_fix[_sat] must fit within 32 bits");
4686 if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat) {
4687 Assert(
4688 Op3->getZExtValue() < Op1->getType()->getScalarSizeInBits(),
4689 "the scale of smul_fix[_sat] must be less than the width of the operands");
4690 } else {
4691 Assert(Op3->getZExtValue() <= Op1->getType()->getScalarSizeInBits(),
4692 "the scale of umul_fix[_sat] must be less than or equal to the width of "
4693 "the operands");
4695 break;
4697 case Intrinsic::lround:
4698 case Intrinsic::llround:
4699 case Intrinsic::lrint:
4700 case Intrinsic::llrint: {
4701 Type *ValTy = Call.getArgOperand(0)->getType();
4702 Type *ResultTy = Call.getType();
4703 Assert(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
4704 "Intrinsic does not support vectors", &Call);
4705 break;
4710 /// Carefully grab the subprogram from a local scope.
4712 /// This carefully grabs the subprogram from a local scope, avoiding the
4713 /// built-in assertions that would typically fire.
4714 static DISubprogram *getSubprogram(Metadata *LocalScope) {
4715 if (!LocalScope)
4716 return nullptr;
4718 if (auto *SP = dyn_cast<DISubprogram>(LocalScope))
4719 return SP;
4721 if (auto *LB = dyn_cast<DILexicalBlockBase>(LocalScope))
4722 return getSubprogram(LB->getRawScope());
4724 // Just return null; broken scope chains are checked elsewhere.
4725 assert(!isa<DILocalScope>(LocalScope) && "Unknown type of local scope");
4726 return nullptr;
4729 void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI) {
4730 unsigned NumOperands = FPI.getNumArgOperands();
4731 bool HasExceptionMD = false;
4732 bool HasRoundingMD = false;
4733 switch (FPI.getIntrinsicID()) {
4734 case Intrinsic::experimental_constrained_sqrt:
4735 case Intrinsic::experimental_constrained_sin:
4736 case Intrinsic::experimental_constrained_cos:
4737 case Intrinsic::experimental_constrained_exp:
4738 case Intrinsic::experimental_constrained_exp2:
4739 case Intrinsic::experimental_constrained_log:
4740 case Intrinsic::experimental_constrained_log10:
4741 case Intrinsic::experimental_constrained_log2:
4742 case Intrinsic::experimental_constrained_rint:
4743 case Intrinsic::experimental_constrained_nearbyint:
4744 case Intrinsic::experimental_constrained_ceil:
4745 case Intrinsic::experimental_constrained_floor:
4746 case Intrinsic::experimental_constrained_round:
4747 case Intrinsic::experimental_constrained_trunc:
4748 Assert((NumOperands == 3), "invalid arguments for constrained FP intrinsic",
4749 &FPI);
4750 HasExceptionMD = true;
4751 HasRoundingMD = true;
4752 break;
4754 case Intrinsic::experimental_constrained_fma:
4755 Assert((NumOperands == 5), "invalid arguments for constrained FP intrinsic",
4756 &FPI);
4757 HasExceptionMD = true;
4758 HasRoundingMD = true;
4759 break;
4761 case Intrinsic::experimental_constrained_fadd:
4762 case Intrinsic::experimental_constrained_fsub:
4763 case Intrinsic::experimental_constrained_fmul:
4764 case Intrinsic::experimental_constrained_fdiv:
4765 case Intrinsic::experimental_constrained_frem:
4766 case Intrinsic::experimental_constrained_pow:
4767 case Intrinsic::experimental_constrained_powi:
4768 case Intrinsic::experimental_constrained_maxnum:
4769 case Intrinsic::experimental_constrained_minnum:
4770 Assert((NumOperands == 4), "invalid arguments for constrained FP intrinsic",
4771 &FPI);
4772 HasExceptionMD = true;
4773 HasRoundingMD = true;
4774 break;
4776 case Intrinsic::experimental_constrained_fptrunc:
4777 case Intrinsic::experimental_constrained_fpext: {
4778 if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
4779 Assert((NumOperands == 3),
4780 "invalid arguments for constrained FP intrinsic", &FPI);
4781 HasRoundingMD = true;
4782 } else {
4783 Assert((NumOperands == 2),
4784 "invalid arguments for constrained FP intrinsic", &FPI);
4786 HasExceptionMD = true;
4788 Value *Operand = FPI.getArgOperand(0);
4789 Type *OperandTy = Operand->getType();
4790 Value *Result = &FPI;
4791 Type *ResultTy = Result->getType();
4792 Assert(OperandTy->isFPOrFPVectorTy(),
4793 "Intrinsic first argument must be FP or FP vector", &FPI);
4794 Assert(ResultTy->isFPOrFPVectorTy(),
4795 "Intrinsic result must be FP or FP vector", &FPI);
4796 Assert(OperandTy->isVectorTy() == ResultTy->isVectorTy(),
4797 "Intrinsic first argument and result disagree on vector use", &FPI);
4798 if (OperandTy->isVectorTy()) {
4799 auto *OperandVecTy = cast<VectorType>(OperandTy);
4800 auto *ResultVecTy = cast<VectorType>(ResultTy);
4801 Assert(OperandVecTy->getNumElements() == ResultVecTy->getNumElements(),
4802 "Intrinsic first argument and result vector lengths must be equal",
4803 &FPI);
4805 if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
4806 Assert(OperandTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits(),
4807 "Intrinsic first argument's type must be larger than result type",
4808 &FPI);
4809 } else {
4810 Assert(OperandTy->getScalarSizeInBits() < ResultTy->getScalarSizeInBits(),
4811 "Intrinsic first argument's type must be smaller than result type",
4812 &FPI);
4815 break;
4817 default:
4818 llvm_unreachable("Invalid constrained FP intrinsic!");
4821 // If a non-metadata argument is passed in a metadata slot then the
4822 // error will be caught earlier when the incorrect argument doesn't
4823 // match the specification in the intrinsic call table. Thus, no
4824 // argument type check is needed here.
4826 if (HasExceptionMD) {
4827 Assert(FPI.getExceptionBehavior().hasValue(),
4828 "invalid exception behavior argument", &FPI);
4830 if (HasRoundingMD) {
4831 Assert(FPI.getRoundingMode().hasValue(),
4832 "invalid rounding mode argument", &FPI);
4836 void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) {
4837 auto *MD = cast<MetadataAsValue>(DII.getArgOperand(0))->getMetadata();
4838 AssertDI(isa<ValueAsMetadata>(MD) ||
4839 (isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
4840 "invalid llvm.dbg." + Kind + " intrinsic address/value", &DII, MD);
4841 AssertDI(isa<DILocalVariable>(DII.getRawVariable()),
4842 "invalid llvm.dbg." + Kind + " intrinsic variable", &DII,
4843 DII.getRawVariable());
4844 AssertDI(isa<DIExpression>(DII.getRawExpression()),
4845 "invalid llvm.dbg." + Kind + " intrinsic expression", &DII,
4846 DII.getRawExpression());
4848 // Ignore broken !dbg attachments; they're checked elsewhere.
4849 if (MDNode *N = DII.getDebugLoc().getAsMDNode())
4850 if (!isa<DILocation>(N))
4851 return;
4853 BasicBlock *BB = DII.getParent();
4854 Function *F = BB ? BB->getParent() : nullptr;
4856 // The scopes for variables and !dbg attachments must agree.
4857 DILocalVariable *Var = DII.getVariable();
4858 DILocation *Loc = DII.getDebugLoc();
4859 AssertDI(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
4860 &DII, BB, F);
4862 DISubprogram *VarSP = getSubprogram(Var->getRawScope());
4863 DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
4864 if (!VarSP || !LocSP)
4865 return; // Broken scope chains are checked elsewhere.
4867 AssertDI(VarSP == LocSP, "mismatched subprogram between llvm.dbg." + Kind +
4868 " variable and !dbg attachment",
4869 &DII, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
4870 Loc->getScope()->getSubprogram());
4872 // This check is redundant with one in visitLocalVariable().
4873 AssertDI(isType(Var->getRawType()), "invalid type ref", Var,
4874 Var->getRawType());
4875 if (auto *Type = dyn_cast_or_null<DIType>(Var->getRawType()))
4876 if (Type->isBlockByrefStruct())
4877 AssertDI(DII.getExpression() && DII.getExpression()->getNumElements(),
4878 "BlockByRef variable without complex expression", Var, &DII);
4880 verifyFnArgs(DII);
4883 void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {
4884 AssertDI(isa<DILabel>(DLI.getRawLabel()),
4885 "invalid llvm.dbg." + Kind + " intrinsic variable", &DLI,
4886 DLI.getRawLabel());
4888 // Ignore broken !dbg attachments; they're checked elsewhere.
4889 if (MDNode *N = DLI.getDebugLoc().getAsMDNode())
4890 if (!isa<DILocation>(N))
4891 return;
4893 BasicBlock *BB = DLI.getParent();
4894 Function *F = BB ? BB->getParent() : nullptr;
4896 // The scopes for variables and !dbg attachments must agree.
4897 DILabel *Label = DLI.getLabel();
4898 DILocation *Loc = DLI.getDebugLoc();
4899 Assert(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
4900 &DLI, BB, F);
4902 DISubprogram *LabelSP = getSubprogram(Label->getRawScope());
4903 DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
4904 if (!LabelSP || !LocSP)
4905 return;
4907 AssertDI(LabelSP == LocSP, "mismatched subprogram between llvm.dbg." + Kind +
4908 " label and !dbg attachment",
4909 &DLI, BB, F, Label, Label->getScope()->getSubprogram(), Loc,
4910 Loc->getScope()->getSubprogram());
4913 void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic &I) {
4914 DILocalVariable *V = dyn_cast_or_null<DILocalVariable>(I.getRawVariable());
4915 DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
4917 // We don't know whether this intrinsic verified correctly.
4918 if (!V || !E || !E->isValid())
4919 return;
4921 // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
4922 auto Fragment = E->getFragmentInfo();
4923 if (!Fragment)
4924 return;
4926 // The frontend helps out GDB by emitting the members of local anonymous
4927 // unions as artificial local variables with shared storage. When SROA splits
4928 // the storage for artificial local variables that are smaller than the entire
4929 // union, the overhang piece will be outside of the allotted space for the
4930 // variable and this check fails.
4931 // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
4932 if (V->isArtificial())
4933 return;
4935 verifyFragmentExpression(*V, *Fragment, &I);
4938 template <typename ValueOrMetadata>
4939 void Verifier::verifyFragmentExpression(const DIVariable &V,
4940 DIExpression::FragmentInfo Fragment,
4941 ValueOrMetadata *Desc) {
4942 // If there's no size, the type is broken, but that should be checked
4943 // elsewhere.
4944 auto VarSize = V.getSizeInBits();
4945 if (!VarSize)
4946 return;
4948 unsigned FragSize = Fragment.SizeInBits;
4949 unsigned FragOffset = Fragment.OffsetInBits;
4950 AssertDI(FragSize + FragOffset <= *VarSize,
4951 "fragment is larger than or outside of variable", Desc, &V);
4952 AssertDI(FragSize != *VarSize, "fragment covers entire variable", Desc, &V);
4955 void Verifier::verifyFnArgs(const DbgVariableIntrinsic &I) {
4956 // This function does not take the scope of noninlined function arguments into
4957 // account. Don't run it if current function is nodebug, because it may
4958 // contain inlined debug intrinsics.
4959 if (!HasDebugInfo)
4960 return;
4962 // For performance reasons only check non-inlined ones.
4963 if (I.getDebugLoc()->getInlinedAt())
4964 return;
4966 DILocalVariable *Var = I.getVariable();
4967 AssertDI(Var, "dbg intrinsic without variable");
4969 unsigned ArgNo = Var->getArg();
4970 if (!ArgNo)
4971 return;
4973 // Verify there are no duplicate function argument debug info entries.
4974 // These will cause hard-to-debug assertions in the DWARF backend.
4975 if (DebugFnArgs.size() < ArgNo)
4976 DebugFnArgs.resize(ArgNo, nullptr);
4978 auto *Prev = DebugFnArgs[ArgNo - 1];
4979 DebugFnArgs[ArgNo - 1] = Var;
4980 AssertDI(!Prev || (Prev == Var), "conflicting debug info for argument", &I,
4981 Prev, Var);
4984 void Verifier::verifyCompileUnits() {
4985 // When more than one Module is imported into the same context, such as during
4986 // an LTO build before linking the modules, ODR type uniquing may cause types
4987 // to point to a different CU. This check does not make sense in this case.
4988 if (M.getContext().isODRUniquingDebugTypes())
4989 return;
4990 auto *CUs = M.getNamedMetadata("llvm.dbg.cu");
4991 SmallPtrSet<const Metadata *, 2> Listed;
4992 if (CUs)
4993 Listed.insert(CUs->op_begin(), CUs->op_end());
4994 for (auto *CU : CUVisited)
4995 AssertDI(Listed.count(CU), "DICompileUnit not listed in llvm.dbg.cu", CU);
4996 CUVisited.clear();
4999 void Verifier::verifyDeoptimizeCallingConvs() {
5000 if (DeoptimizeDeclarations.empty())
5001 return;
5003 const Function *First = DeoptimizeDeclarations[0];
5004 for (auto *F : makeArrayRef(DeoptimizeDeclarations).slice(1)) {
5005 Assert(First->getCallingConv() == F->getCallingConv(),
5006 "All llvm.experimental.deoptimize declarations must have the same "
5007 "calling convention",
5008 First, F);
5012 void Verifier::verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F) {
5013 bool HasSource = F.getSource().hasValue();
5014 if (!HasSourceDebugInfo.count(&U))
5015 HasSourceDebugInfo[&U] = HasSource;
5016 AssertDI(HasSource == HasSourceDebugInfo[&U],
5017 "inconsistent use of embedded source");
5020 //===----------------------------------------------------------------------===//
5021 // Implement the public interfaces to this file...
5022 //===----------------------------------------------------------------------===//
5024 bool llvm::verifyFunction(const Function &f, raw_ostream *OS) {
5025 Function &F = const_cast<Function &>(f);
5027 // Don't use a raw_null_ostream. Printing IR is expensive.
5028 Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f.getParent());
5030 // Note that this function's return value is inverted from what you would
5031 // expect of a function called "verify".
5032 return !V.verify(F);
5035 bool llvm::verifyModule(const Module &M, raw_ostream *OS,
5036 bool *BrokenDebugInfo) {
5037 // Don't use a raw_null_ostream. Printing IR is expensive.
5038 Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo, M);
5040 bool Broken = false;
5041 for (const Function &F : M)
5042 Broken |= !V.verify(F);
5044 Broken |= !V.verify();
5045 if (BrokenDebugInfo)
5046 *BrokenDebugInfo = V.hasBrokenDebugInfo();
5047 // Note that this function's return value is inverted from what you would
5048 // expect of a function called "verify".
5049 return Broken;
5052 namespace {
5054 struct VerifierLegacyPass : public FunctionPass {
5055 static char ID;
5057 std::unique_ptr<Verifier> V;
5058 bool FatalErrors = true;
5060 VerifierLegacyPass() : FunctionPass(ID) {
5061 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
5063 explicit VerifierLegacyPass(bool FatalErrors)
5064 : FunctionPass(ID),
5065 FatalErrors(FatalErrors) {
5066 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
5069 bool doInitialization(Module &M) override {
5070 V = std::make_unique<Verifier>(
5071 &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M);
5072 return false;
5075 bool runOnFunction(Function &F) override {
5076 if (!V->verify(F) && FatalErrors) {
5077 errs() << "in function " << F.getName() << '\n';
5078 report_fatal_error("Broken function found, compilation aborted!");
5080 return false;
5083 bool doFinalization(Module &M) override {
5084 bool HasErrors = false;
5085 for (Function &F : M)
5086 if (F.isDeclaration())
5087 HasErrors |= !V->verify(F);
5089 HasErrors |= !V->verify();
5090 if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo()))
5091 report_fatal_error("Broken module found, compilation aborted!");
5092 return false;
5095 void getAnalysisUsage(AnalysisUsage &AU) const override {
5096 AU.setPreservesAll();
5100 } // end anonymous namespace
5102 /// Helper to issue failure from the TBAA verification
5103 template <typename... Tys> void TBAAVerifier::CheckFailed(Tys &&... Args) {
5104 if (Diagnostic)
5105 return Diagnostic->CheckFailed(Args...);
5108 #define AssertTBAA(C, ...) \
5109 do { \
5110 if (!(C)) { \
5111 CheckFailed(__VA_ARGS__); \
5112 return false; \
5114 } while (false)
5116 /// Verify that \p BaseNode can be used as the "base type" in the struct-path
5117 /// TBAA scheme. This means \p BaseNode is either a scalar node, or a
5118 /// struct-type node describing an aggregate data structure (like a struct).
5119 TBAAVerifier::TBAABaseNodeSummary
5120 TBAAVerifier::verifyTBAABaseNode(Instruction &I, const MDNode *BaseNode,
5121 bool IsNewFormat) {
5122 if (BaseNode->getNumOperands() < 2) {
5123 CheckFailed("Base nodes must have at least two operands", &I, BaseNode);
5124 return {true, ~0u};
5127 auto Itr = TBAABaseNodes.find(BaseNode);
5128 if (Itr != TBAABaseNodes.end())
5129 return Itr->second;
5131 auto Result = verifyTBAABaseNodeImpl(I, BaseNode, IsNewFormat);
5132 auto InsertResult = TBAABaseNodes.insert({BaseNode, Result});
5133 (void)InsertResult;
5134 assert(InsertResult.second && "We just checked!");
5135 return Result;
5138 TBAAVerifier::TBAABaseNodeSummary
5139 TBAAVerifier::verifyTBAABaseNodeImpl(Instruction &I, const MDNode *BaseNode,
5140 bool IsNewFormat) {
5141 const TBAAVerifier::TBAABaseNodeSummary InvalidNode = {true, ~0u};
5143 if (BaseNode->getNumOperands() == 2) {
5144 // Scalar nodes can only be accessed at offset 0.
5145 return isValidScalarTBAANode(BaseNode)
5146 ? TBAAVerifier::TBAABaseNodeSummary({false, 0})
5147 : InvalidNode;
5150 if (IsNewFormat) {
5151 if (BaseNode->getNumOperands() % 3 != 0) {
5152 CheckFailed("Access tag nodes must have the number of operands that is a "
5153 "multiple of 3!", BaseNode);
5154 return InvalidNode;
5156 } else {
5157 if (BaseNode->getNumOperands() % 2 != 1) {
5158 CheckFailed("Struct tag nodes must have an odd number of operands!",
5159 BaseNode);
5160 return InvalidNode;
5164 // Check the type size field.
5165 if (IsNewFormat) {
5166 auto *TypeSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
5167 BaseNode->getOperand(1));
5168 if (!TypeSizeNode) {
5169 CheckFailed("Type size nodes must be constants!", &I, BaseNode);
5170 return InvalidNode;
5174 // Check the type name field. In the new format it can be anything.
5175 if (!IsNewFormat && !isa<MDString>(BaseNode->getOperand(0))) {
5176 CheckFailed("Struct tag nodes have a string as their first operand",
5177 BaseNode);
5178 return InvalidNode;
5181 bool Failed = false;
5183 Optional<APInt> PrevOffset;
5184 unsigned BitWidth = ~0u;
5186 // We've already checked that BaseNode is not a degenerate root node with one
5187 // operand in \c verifyTBAABaseNode, so this loop should run at least once.
5188 unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
5189 unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
5190 for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
5191 Idx += NumOpsPerField) {
5192 const MDOperand &FieldTy = BaseNode->getOperand(Idx);
5193 const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1);
5194 if (!isa<MDNode>(FieldTy)) {
5195 CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode);
5196 Failed = true;
5197 continue;
5200 auto *OffsetEntryCI =
5201 mdconst::dyn_extract_or_null<ConstantInt>(FieldOffset);
5202 if (!OffsetEntryCI) {
5203 CheckFailed("Offset entries must be constants!", &I, BaseNode);
5204 Failed = true;
5205 continue;
5208 if (BitWidth == ~0u)
5209 BitWidth = OffsetEntryCI->getBitWidth();
5211 if (OffsetEntryCI->getBitWidth() != BitWidth) {
5212 CheckFailed(
5213 "Bitwidth between the offsets and struct type entries must match", &I,
5214 BaseNode);
5215 Failed = true;
5216 continue;
5219 // NB! As far as I can tell, we generate a non-strictly increasing offset
5220 // sequence only from structs that have zero size bit fields. When
5221 // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
5222 // pick the field lexically the latest in struct type metadata node. This
5223 // mirrors the actual behavior of the alias analysis implementation.
5224 bool IsAscending =
5225 !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue());
5227 if (!IsAscending) {
5228 CheckFailed("Offsets must be increasing!", &I, BaseNode);
5229 Failed = true;
5232 PrevOffset = OffsetEntryCI->getValue();
5234 if (IsNewFormat) {
5235 auto *MemberSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
5236 BaseNode->getOperand(Idx + 2));
5237 if (!MemberSizeNode) {
5238 CheckFailed("Member size entries must be constants!", &I, BaseNode);
5239 Failed = true;
5240 continue;
5245 return Failed ? InvalidNode
5246 : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth);
5249 static bool IsRootTBAANode(const MDNode *MD) {
5250 return MD->getNumOperands() < 2;
5253 static bool IsScalarTBAANodeImpl(const MDNode *MD,
5254 SmallPtrSetImpl<const MDNode *> &Visited) {
5255 if (MD->getNumOperands() != 2 && MD->getNumOperands() != 3)
5256 return false;
5258 if (!isa<MDString>(MD->getOperand(0)))
5259 return false;
5261 if (MD->getNumOperands() == 3) {
5262 auto *Offset = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
5263 if (!(Offset && Offset->isZero() && isa<MDString>(MD->getOperand(0))))
5264 return false;
5267 auto *Parent = dyn_cast_or_null<MDNode>(MD->getOperand(1));
5268 return Parent && Visited.insert(Parent).second &&
5269 (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited));
5272 bool TBAAVerifier::isValidScalarTBAANode(const MDNode *MD) {
5273 auto ResultIt = TBAAScalarNodes.find(MD);
5274 if (ResultIt != TBAAScalarNodes.end())
5275 return ResultIt->second;
5277 SmallPtrSet<const MDNode *, 4> Visited;
5278 bool Result = IsScalarTBAANodeImpl(MD, Visited);
5279 auto InsertResult = TBAAScalarNodes.insert({MD, Result});
5280 (void)InsertResult;
5281 assert(InsertResult.second && "Just checked!");
5283 return Result;
5286 /// Returns the field node at the offset \p Offset in \p BaseNode. Update \p
5287 /// Offset in place to be the offset within the field node returned.
5289 /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
5290 MDNode *TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction &I,
5291 const MDNode *BaseNode,
5292 APInt &Offset,
5293 bool IsNewFormat) {
5294 assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!");
5296 // Scalar nodes have only one possible "field" -- their parent in the access
5297 // hierarchy. Offset must be zero at this point, but our caller is supposed
5298 // to Assert that.
5299 if (BaseNode->getNumOperands() == 2)
5300 return cast<MDNode>(BaseNode->getOperand(1));
5302 unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
5303 unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
5304 for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
5305 Idx += NumOpsPerField) {
5306 auto *OffsetEntryCI =
5307 mdconst::extract<ConstantInt>(BaseNode->getOperand(Idx + 1));
5308 if (OffsetEntryCI->getValue().ugt(Offset)) {
5309 if (Idx == FirstFieldOpNo) {
5310 CheckFailed("Could not find TBAA parent in struct type node", &I,
5311 BaseNode, &Offset);
5312 return nullptr;
5315 unsigned PrevIdx = Idx - NumOpsPerField;
5316 auto *PrevOffsetEntryCI =
5317 mdconst::extract<ConstantInt>(BaseNode->getOperand(PrevIdx + 1));
5318 Offset -= PrevOffsetEntryCI->getValue();
5319 return cast<MDNode>(BaseNode->getOperand(PrevIdx));
5323 unsigned LastIdx = BaseNode->getNumOperands() - NumOpsPerField;
5324 auto *LastOffsetEntryCI = mdconst::extract<ConstantInt>(
5325 BaseNode->getOperand(LastIdx + 1));
5326 Offset -= LastOffsetEntryCI->getValue();
5327 return cast<MDNode>(BaseNode->getOperand(LastIdx));
5330 static bool isNewFormatTBAATypeNode(llvm::MDNode *Type) {
5331 if (!Type || Type->getNumOperands() < 3)
5332 return false;
5334 // In the new format type nodes shall have a reference to the parent type as
5335 // its first operand.
5336 MDNode *Parent = dyn_cast_or_null<MDNode>(Type->getOperand(0));
5337 if (!Parent)
5338 return false;
5340 return true;
5343 bool TBAAVerifier::visitTBAAMetadata(Instruction &I, const MDNode *MD) {
5344 AssertTBAA(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
5345 isa<VAArgInst>(I) || isa<AtomicRMWInst>(I) ||
5346 isa<AtomicCmpXchgInst>(I),
5347 "This instruction shall not have a TBAA access tag!", &I);
5349 bool IsStructPathTBAA =
5350 isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
5352 AssertTBAA(
5353 IsStructPathTBAA,
5354 "Old-style TBAA is no longer allowed, use struct-path TBAA instead", &I);
5356 MDNode *BaseNode = dyn_cast_or_null<MDNode>(MD->getOperand(0));
5357 MDNode *AccessType = dyn_cast_or_null<MDNode>(MD->getOperand(1));
5359 bool IsNewFormat = isNewFormatTBAATypeNode(AccessType);
5361 if (IsNewFormat) {
5362 AssertTBAA(MD->getNumOperands() == 4 || MD->getNumOperands() == 5,
5363 "Access tag metadata must have either 4 or 5 operands", &I, MD);
5364 } else {
5365 AssertTBAA(MD->getNumOperands() < 5,
5366 "Struct tag metadata must have either 3 or 4 operands", &I, MD);
5369 // Check the access size field.
5370 if (IsNewFormat) {
5371 auto *AccessSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
5372 MD->getOperand(3));
5373 AssertTBAA(AccessSizeNode, "Access size field must be a constant", &I, MD);
5376 // Check the immutability flag.
5377 unsigned ImmutabilityFlagOpNo = IsNewFormat ? 4 : 3;
5378 if (MD->getNumOperands() == ImmutabilityFlagOpNo + 1) {
5379 auto *IsImmutableCI = mdconst::dyn_extract_or_null<ConstantInt>(
5380 MD->getOperand(ImmutabilityFlagOpNo));
5381 AssertTBAA(IsImmutableCI,
5382 "Immutability tag on struct tag metadata must be a constant",
5383 &I, MD);
5384 AssertTBAA(
5385 IsImmutableCI->isZero() || IsImmutableCI->isOne(),
5386 "Immutability part of the struct tag metadata must be either 0 or 1",
5387 &I, MD);
5390 AssertTBAA(BaseNode && AccessType,
5391 "Malformed struct tag metadata: base and access-type "
5392 "should be non-null and point to Metadata nodes",
5393 &I, MD, BaseNode, AccessType);
5395 if (!IsNewFormat) {
5396 AssertTBAA(isValidScalarTBAANode(AccessType),
5397 "Access type node must be a valid scalar type", &I, MD,
5398 AccessType);
5401 auto *OffsetCI = mdconst::dyn_extract_or_null<ConstantInt>(MD->getOperand(2));
5402 AssertTBAA(OffsetCI, "Offset must be constant integer", &I, MD);
5404 APInt Offset = OffsetCI->getValue();
5405 bool SeenAccessTypeInPath = false;
5407 SmallPtrSet<MDNode *, 4> StructPath;
5409 for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode);
5410 BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset,
5411 IsNewFormat)) {
5412 if (!StructPath.insert(BaseNode).second) {
5413 CheckFailed("Cycle detected in struct path", &I, MD);
5414 return false;
5417 bool Invalid;
5418 unsigned BaseNodeBitWidth;
5419 std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode,
5420 IsNewFormat);
5422 // If the base node is invalid in itself, then we've already printed all the
5423 // errors we wanted to print.
5424 if (Invalid)
5425 return false;
5427 SeenAccessTypeInPath |= BaseNode == AccessType;
5429 if (isValidScalarTBAANode(BaseNode) || BaseNode == AccessType)
5430 AssertTBAA(Offset == 0, "Offset not zero at the point of scalar access",
5431 &I, MD, &Offset);
5433 AssertTBAA(BaseNodeBitWidth == Offset.getBitWidth() ||
5434 (BaseNodeBitWidth == 0 && Offset == 0) ||
5435 (IsNewFormat && BaseNodeBitWidth == ~0u),
5436 "Access bit-width not the same as description bit-width", &I, MD,
5437 BaseNodeBitWidth, Offset.getBitWidth());
5439 if (IsNewFormat && SeenAccessTypeInPath)
5440 break;
5443 AssertTBAA(SeenAccessTypeInPath, "Did not see access type in access path!",
5444 &I, MD);
5445 return true;
5448 char VerifierLegacyPass::ID = 0;
5449 INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
5451 FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
5452 return new VerifierLegacyPass(FatalErrors);
5455 AnalysisKey VerifierAnalysis::Key;
5456 VerifierAnalysis::Result VerifierAnalysis::run(Module &M,
5457 ModuleAnalysisManager &) {
5458 Result Res;
5459 Res.IRBroken = llvm::verifyModule(M, &dbgs(), &Res.DebugInfoBroken);
5460 return Res;
5463 VerifierAnalysis::Result VerifierAnalysis::run(Function &F,
5464 FunctionAnalysisManager &) {
5465 return { llvm::verifyFunction(F, &dbgs()), false };
5468 PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
5469 auto Res = AM.getResult<VerifierAnalysis>(M);
5470 if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
5471 report_fatal_error("Broken module found, compilation aborted!");
5473 return PreservedAnalyses::all();
5476 PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
5477 auto res = AM.getResult<VerifierAnalysis>(F);
5478 if (res.IRBroken && FatalErrors)
5479 report_fatal_error("Broken function found, compilation aborted!");
5481 return PreservedAnalyses::all();