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 // basic correctness checking of input to the system.
11 //
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.
14 //
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 // * All basic blocks should only end with terminator insts, not contain them
27 // * The entry node to a function must not have predecessors
28 // * All Instructions must be embedded into a basic block
29 // * Functions cannot take a void-typed parameter
30 // * Verify that a function's argument list agrees with it's declared type.
31 // * It is illegal to specify a name for a void value.
32 // * It is illegal to have a internal global value with no initializer
33 // * It is illegal to have a ret instruction that returns a value that does not
34 // agree with the function return value type.
35 // * Function call argument types match the function prototype
36 // * A landing pad is defined by a landingpad instruction, and can be jumped to
37 // only by the unwind edge of an invoke instruction.
38 // * A landingpad instruction must be the first non-PHI instruction in the
39 // block.
40 // * Landingpad instructions must be in a function with a personality function.
41 // * Convergence control intrinsics are introduced in ConvergentOperations.rst.
42 // The applied restrictions are too numerous to list here.
43 // * The convergence entry intrinsic and the loop heart must be the first
44 // non-PHI instruction in their respective block. This does not conflict with
45 // the landing pads, since these two kinds cannot occur in the same block.
46 // * All other things that are tested by asserts spread about the code...
47 //
48 //===----------------------------------------------------------------------===//
49
50 #include "llvm/IR/Verifier.h"
51 #include "llvm/ADT/APFloat.h"
52 #include "llvm/ADT/APInt.h"
53 #include "llvm/ADT/ArrayRef.h"
54 #include "llvm/ADT/DenseMap.h"
55 #include "llvm/ADT/MapVector.h"
56 #include "llvm/ADT/PostOrderIterator.h"
57 #include "llvm/ADT/STLExtras.h"
58 #include "llvm/ADT/SmallPtrSet.h"
59 #include "llvm/ADT/SmallSet.h"
60 #include "llvm/ADT/SmallVector.h"
61 #include "llvm/ADT/StringExtras.h"
62 #include "llvm/ADT/StringMap.h"
63 #include "llvm/ADT/StringRef.h"
64 #include "llvm/ADT/Twine.h"
65 #include "llvm/BinaryFormat/Dwarf.h"
66 #include "llvm/IR/Argument.h"
67 #include "llvm/IR/AttributeMask.h"
68 #include "llvm/IR/Attributes.h"
69 #include "llvm/IR/BasicBlock.h"
70 #include "llvm/IR/CFG.h"
71 #include "llvm/IR/CallingConv.h"
72 #include "llvm/IR/Comdat.h"
73 #include "llvm/IR/Constant.h"
74 #include "llvm/IR/ConstantRange.h"
75 #include "llvm/IR/Constants.h"
76 #include "llvm/IR/ConvergenceVerifier.h"
77 #include "llvm/IR/DataLayout.h"
78 #include "llvm/IR/DebugInfo.h"
79 #include "llvm/IR/DebugInfoMetadata.h"
80 #include "llvm/IR/DebugLoc.h"
81 #include "llvm/IR/DerivedTypes.h"
82 #include "llvm/IR/Dominators.h"
83 #include "llvm/IR/EHPersonalities.h"
84 #include "llvm/IR/Function.h"
85 #include "llvm/IR/GCStrategy.h"
86 #include "llvm/IR/GlobalAlias.h"
87 #include "llvm/IR/GlobalValue.h"
88 #include "llvm/IR/GlobalVariable.h"
89 #include "llvm/IR/InlineAsm.h"
90 #include "llvm/IR/InstVisitor.h"
91 #include "llvm/IR/InstrTypes.h"
92 #include "llvm/IR/Instruction.h"
93 #include "llvm/IR/Instructions.h"
94 #include "llvm/IR/IntrinsicInst.h"
95 #include "llvm/IR/Intrinsics.h"
96 #include "llvm/IR/IntrinsicsAArch64.h"
97 #include "llvm/IR/IntrinsicsAMDGPU.h"
98 #include "llvm/IR/IntrinsicsARM.h"
99 #include "llvm/IR/IntrinsicsNVPTX.h"
100 #include "llvm/IR/IntrinsicsWebAssembly.h"
101 #include "llvm/IR/LLVMContext.h"
102 #include "llvm/IR/Metadata.h"
103 #include "llvm/IR/Module.h"
104 #include "llvm/IR/ModuleSlotTracker.h"
105 #include "llvm/IR/PassManager.h"
106 #include "llvm/IR/Statepoint.h"
107 #include "llvm/IR/Type.h"
108 #include "llvm/IR/Use.h"
109 #include "llvm/IR/User.h"
110 #include "llvm/IR/VFABIDemangler.h"
111 #include "llvm/IR/Value.h"
112 #include "llvm/InitializePasses.h"
113 #include "llvm/Pass.h"
114 #include "llvm/Support/AtomicOrdering.h"
115 #include "llvm/Support/Casting.h"
116 #include "llvm/Support/CommandLine.h"
117 #include "llvm/Support/ErrorHandling.h"
118 #include "llvm/Support/MathExtras.h"
119 #include "llvm/Support/raw_ostream.h"
120 #include <algorithm>
121 #include <cassert>
122 #include <cstdint>
123 #include <memory>
124 #include <optional>
125 #include <string>
126 #include <utility>
127
128 using namespace llvm;
129
130 static cl::opt<bool> VerifyNoAliasScopeDomination(
131 "verify-noalias-scope-decl-dom", cl::Hidden, cl::init(false),
132 cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
133 "scopes are not dominating"));
134
135 namespace llvm {
136
137 struct VerifierSupport {
138 raw_ostream *OS;
139 const Module &M;
140 ModuleSlotTracker MST;
141 Triple TT;
142 const DataLayout &DL;
143 LLVMContext &Context;
144
145 /// Track the brokenness of the module while recursively visiting.
146 bool Broken = false;
147 /// Broken debug info can be "recovered" from by stripping the debug info.
148 bool BrokenDebugInfo = false;
149 /// Whether to treat broken debug info as an error.
150 bool TreatBrokenDebugInfoAsError = true;
151
VerifierSupportllvm::VerifierSupport152 explicit VerifierSupport(raw_ostream *OS, const Module &M)
153 : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
154 Context(M.getContext()) {}
155
156 private:
Writellvm::VerifierSupport157 void Write(const Module *M) {
158 *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
159 }
160
Writellvm::VerifierSupport161 void Write(const Value *V) {
162 if (V)
163 Write(*V);
164 }
165
Writellvm::VerifierSupport166 void Write(const Value &V) {
167 if (isa<Instruction>(V)) {
168 V.print(*OS, MST);
169 *OS << '\n';
170 } else {
171 V.printAsOperand(*OS, true, MST);
172 *OS << '\n';
173 }
174 }
175
Writellvm::VerifierSupport176 void Write(const DPValue *V) {
177 if (V)
178 V->print(*OS, MST, false);
179 }
180
Writellvm::VerifierSupport181 void Write(const Metadata *MD) {
182 if (!MD)
183 return;
184 MD->print(*OS, MST, &M);
185 *OS << '\n';
186 }
187
Writellvm::VerifierSupport188 template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
189 Write(MD.get());
190 }
191
Writellvm::VerifierSupport192 void Write(const NamedMDNode *NMD) {
193 if (!NMD)
194 return;
195 NMD->print(*OS, MST);
196 *OS << '\n';
197 }
198
Writellvm::VerifierSupport199 void Write(Type *T) {
200 if (!T)
201 return;
202 *OS << ' ' << *T;
203 }
204
Writellvm::VerifierSupport205 void Write(const Comdat *C) {
206 if (!C)
207 return;
208 *OS << *C;
209 }
210
Writellvm::VerifierSupport211 void Write(const APInt *AI) {
212 if (!AI)
213 return;
214 *OS << *AI << '\n';
215 }
216
Writellvm::VerifierSupport217 void Write(const unsigned i) { *OS << i << '\n'; }
218
219 // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport220 void Write(const Attribute *A) {
221 if (!A)
222 return;
223 *OS << A->getAsString() << '\n';
224 }
225
226 // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport227 void Write(const AttributeSet *AS) {
228 if (!AS)
229 return;
230 *OS << AS->getAsString() << '\n';
231 }
232
233 // NOLINTNEXTLINE(readability-identifier-naming)
Writellvm::VerifierSupport234 void Write(const AttributeList *AL) {
235 if (!AL)
236 return;
237 AL->print(*OS);
238 }
239
Writellvm::VerifierSupport240 void Write(Printable P) { *OS << P << '\n'; }
241
Writellvm::VerifierSupport242 template <typename T> void Write(ArrayRef<T> Vs) {
243 for (const T &V : Vs)
244 Write(V);
245 }
246
247 template <typename T1, typename... Ts>
WriteTsllvm::VerifierSupport248 void WriteTs(const T1 &V1, const Ts &... Vs) {
249 Write(V1);
250 WriteTs(Vs...);
251 }
252
WriteTsllvm::VerifierSupport253 template <typename... Ts> void WriteTs() {}
254
255 public:
256 /// A check failed, so printout out the condition and the message.
257 ///
258 /// This provides a nice place to put a breakpoint if you want to see why
259 /// something is not correct.
CheckFailedllvm::VerifierSupport260 void CheckFailed(const Twine &Message) {
261 if (OS)
262 *OS << Message << '\n';
263 Broken = true;
264 }
265
266 /// A check failed (with values to print).
267 ///
268 /// This calls the Message-only version so that the above is easier to set a
269 /// breakpoint on.
270 template <typename T1, typename... Ts>
CheckFailedllvm::VerifierSupport271 void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
272 CheckFailed(Message);
273 if (OS)
274 WriteTs(V1, Vs...);
275 }
276
277 /// A debug info check failed.
DebugInfoCheckFailedllvm::VerifierSupport278 void DebugInfoCheckFailed(const Twine &Message) {
279 if (OS)
280 *OS << Message << '\n';
281 Broken |= TreatBrokenDebugInfoAsError;
282 BrokenDebugInfo = true;
283 }
284
285 /// A debug info check failed (with values to print).
286 template <typename T1, typename... Ts>
DebugInfoCheckFailedllvm::VerifierSupport287 void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
288 const Ts &... Vs) {
289 DebugInfoCheckFailed(Message);
290 if (OS)
291 WriteTs(V1, Vs...);
292 }
293 };
294
295 } // namespace llvm
296
297 namespace {
298
299 class Verifier : public InstVisitor<Verifier>, VerifierSupport {
300 friend class InstVisitor<Verifier>;
301
302 // ISD::ArgFlagsTy::MemAlign only have 4 bits for alignment, so
303 // the alignment size should not exceed 2^15. Since encode(Align)
304 // would plus the shift value by 1, the alignment size should
305 // not exceed 2^14, otherwise it can NOT be properly lowered
306 // in backend.
307 static constexpr unsigned ParamMaxAlignment = 1 << 14;
308 DominatorTree DT;
309
310 /// When verifying a basic block, keep track of all of the
311 /// instructions we have seen so far.
312 ///
313 /// This allows us to do efficient dominance checks for the case when an
314 /// instruction has an operand that is an instruction in the same block.
315 SmallPtrSet<Instruction *, 16> InstsInThisBlock;
316
317 /// Keep track of the metadata nodes that have been checked already.
318 SmallPtrSet<const Metadata *, 32> MDNodes;
319
320 /// Keep track which DISubprogram is attached to which function.
321 DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments;
322
323 /// Track all DICompileUnits visited.
324 SmallPtrSet<const Metadata *, 2> CUVisited;
325
326 /// The result type for a landingpad.
327 Type *LandingPadResultTy;
328
329 /// Whether we've seen a call to @llvm.localescape in this function
330 /// already.
331 bool SawFrameEscape;
332
333 /// Whether the current function has a DISubprogram attached to it.
334 bool HasDebugInfo = false;
335
336 /// The current source language.
337 dwarf::SourceLanguage CurrentSourceLang = dwarf::DW_LANG_lo_user;
338
339 /// Stores the count of how many objects were passed to llvm.localescape for a
340 /// given function and the largest index passed to llvm.localrecover.
341 DenseMap<Function *, std::pair<unsigned, unsigned>> FrameEscapeInfo;
342
343 // Maps catchswitches and cleanuppads that unwind to siblings to the
344 // terminators that indicate the unwind, used to detect cycles therein.
345 MapVector<Instruction *, Instruction *> SiblingFuncletInfo;
346
347 /// Cache which blocks are in which funclet, if an EH funclet personality is
348 /// in use. Otherwise empty.
349 DenseMap<BasicBlock *, ColorVector> BlockEHFuncletColors;
350
351 /// Cache of constants visited in search of ConstantExprs.
352 SmallPtrSet<const Constant *, 32> ConstantExprVisited;
353
354 /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
355 SmallVector<const Function *, 4> DeoptimizeDeclarations;
356
357 /// Cache of attribute lists verified.
358 SmallPtrSet<const void *, 32> AttributeListsVisited;
359
360 // Verify that this GlobalValue is only used in this module.
361 // This map is used to avoid visiting uses twice. We can arrive at a user
362 // twice, if they have multiple operands. In particular for very large
363 // constant expressions, we can arrive at a particular user many times.
364 SmallPtrSet<const Value *, 32> GlobalValueVisited;
365
366 // Keeps track of duplicate function argument debug info.
367 SmallVector<const DILocalVariable *, 16> DebugFnArgs;
368
369 TBAAVerifier TBAAVerifyHelper;
370 ConvergenceVerifier ConvergenceVerifyHelper;
371
372 SmallVector<IntrinsicInst *, 4> NoAliasScopeDecls;
373
374 void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
375
376 public:
Verifier(raw_ostream * OS,bool ShouldTreatBrokenDebugInfoAsError,const Module & M)377 explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
378 const Module &M)
379 : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
380 SawFrameEscape(false), TBAAVerifyHelper(this) {
381 TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
382 }
383
hasBrokenDebugInfo() const384 bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
385
verify(const Function & F)386 bool verify(const Function &F) {
387 assert(F.getParent() == &M &&
388 "An instance of this class only works with a specific module!");
389
390 // First ensure the function is well-enough formed to compute dominance
391 // information, and directly compute a dominance tree. We don't rely on the
392 // pass manager to provide this as it isolates us from a potentially
393 // out-of-date dominator tree and makes it significantly more complex to run
394 // this code outside of a pass manager.
395 // FIXME: It's really gross that we have to cast away constness here.
396 if (!F.empty())
397 DT.recalculate(const_cast<Function &>(F));
398
399 for (const BasicBlock &BB : F) {
400 if (!BB.empty() && BB.back().isTerminator())
401 continue;
402
403 if (OS) {
404 *OS << "Basic Block in function '" << F.getName()
405 << "' does not have terminator!\n";
406 BB.printAsOperand(*OS, true, MST);
407 *OS << "\n";
408 }
409 return false;
410 }
411
412 auto FailureCB = [this](const Twine &Message) {
413 this->CheckFailed(Message);
414 };
415 ConvergenceVerifyHelper.initialize(OS, FailureCB, F);
416
417 Broken = false;
418 // FIXME: We strip const here because the inst visitor strips const.
419 visit(const_cast<Function &>(F));
420 verifySiblingFuncletUnwinds();
421
422 if (ConvergenceVerifyHelper.sawTokens())
423 ConvergenceVerifyHelper.verify(DT);
424
425 InstsInThisBlock.clear();
426 DebugFnArgs.clear();
427 LandingPadResultTy = nullptr;
428 SawFrameEscape = false;
429 SiblingFuncletInfo.clear();
430 verifyNoAliasScopeDecl();
431 NoAliasScopeDecls.clear();
432
433 return !Broken;
434 }
435
436 /// Verify the module that this instance of \c Verifier was initialized with.
verify()437 bool verify() {
438 Broken = false;
439
440 // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
441 for (const Function &F : M)
442 if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
443 DeoptimizeDeclarations.push_back(&F);
444
445 // Now that we've visited every function, verify that we never asked to
446 // recover a frame index that wasn't escaped.
447 verifyFrameRecoverIndices();
448 for (const GlobalVariable &GV : M.globals())
449 visitGlobalVariable(GV);
450
451 for (const GlobalAlias &GA : M.aliases())
452 visitGlobalAlias(GA);
453
454 for (const GlobalIFunc &GI : M.ifuncs())
455 visitGlobalIFunc(GI);
456
457 for (const NamedMDNode &NMD : M.named_metadata())
458 visitNamedMDNode(NMD);
459
460 for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
461 visitComdat(SMEC.getValue());
462
463 visitModuleFlags();
464 visitModuleIdents();
465 visitModuleCommandLines();
466
467 verifyCompileUnits();
468
469 verifyDeoptimizeCallingConvs();
470 DISubprogramAttachments.clear();
471 return !Broken;
472 }
473
474 private:
475 /// Whether a metadata node is allowed to be, or contain, a DILocation.
476 enum class AreDebugLocsAllowed { No, Yes };
477
478 // Verification methods...
479 void visitGlobalValue(const GlobalValue &GV);
480 void visitGlobalVariable(const GlobalVariable &GV);
481 void visitGlobalAlias(const GlobalAlias &GA);
482 void visitGlobalIFunc(const GlobalIFunc &GI);
483 void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
484 void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
485 const GlobalAlias &A, const Constant &C);
486 void visitNamedMDNode(const NamedMDNode &NMD);
487 void visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs);
488 void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
489 void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
490 void visitDIArgList(const DIArgList &AL, Function *F);
491 void visitComdat(const Comdat &C);
492 void visitModuleIdents();
493 void visitModuleCommandLines();
494 void visitModuleFlags();
495 void visitModuleFlag(const MDNode *Op,
496 DenseMap<const MDString *, const MDNode *> &SeenIDs,
497 SmallVectorImpl<const MDNode *> &Requirements);
498 void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
499 void visitFunction(const Function &F);
500 void visitBasicBlock(BasicBlock &BB);
501 void verifyRangeMetadata(const Value &V, const MDNode *Range, Type *Ty,
502 bool IsAbsoluteSymbol);
503 void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
504 void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
505 void visitProfMetadata(Instruction &I, MDNode *MD);
506 void visitCallStackMetadata(MDNode *MD);
507 void visitMemProfMetadata(Instruction &I, MDNode *MD);
508 void visitCallsiteMetadata(Instruction &I, MDNode *MD);
509 void visitDIAssignIDMetadata(Instruction &I, MDNode *MD);
510 void visitAnnotationMetadata(MDNode *Annotation);
511 void visitAliasScopeMetadata(const MDNode *MD);
512 void visitAliasScopeListMetadata(const MDNode *MD);
513 void visitAccessGroupMetadata(const MDNode *MD);
514
515 template <class Ty> bool isValidMetadataArray(const MDTuple &N);
516 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
517 #include "llvm/IR/Metadata.def"
518 void visitDIScope(const DIScope &N);
519 void visitDIVariable(const DIVariable &N);
520 void visitDILexicalBlockBase(const DILexicalBlockBase &N);
521 void visitDITemplateParameter(const DITemplateParameter &N);
522
523 void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
524
525 // InstVisitor overrides...
526 using InstVisitor<Verifier>::visit;
527 void visit(Instruction &I);
528
529 void visitTruncInst(TruncInst &I);
530 void visitZExtInst(ZExtInst &I);
531 void visitSExtInst(SExtInst &I);
532 void visitFPTruncInst(FPTruncInst &I);
533 void visitFPExtInst(FPExtInst &I);
534 void visitFPToUIInst(FPToUIInst &I);
535 void visitFPToSIInst(FPToSIInst &I);
536 void visitUIToFPInst(UIToFPInst &I);
537 void visitSIToFPInst(SIToFPInst &I);
538 void visitIntToPtrInst(IntToPtrInst &I);
539 void visitPtrToIntInst(PtrToIntInst &I);
540 void visitBitCastInst(BitCastInst &I);
541 void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
542 void visitPHINode(PHINode &PN);
543 void visitCallBase(CallBase &Call);
544 void visitUnaryOperator(UnaryOperator &U);
545 void visitBinaryOperator(BinaryOperator &B);
546 void visitICmpInst(ICmpInst &IC);
547 void visitFCmpInst(FCmpInst &FC);
548 void visitExtractElementInst(ExtractElementInst &EI);
549 void visitInsertElementInst(InsertElementInst &EI);
550 void visitShuffleVectorInst(ShuffleVectorInst &EI);
visitVAArgInst(VAArgInst & VAA)551 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
552 void visitCallInst(CallInst &CI);
553 void visitInvokeInst(InvokeInst &II);
554 void visitGetElementPtrInst(GetElementPtrInst &GEP);
555 void visitLoadInst(LoadInst &LI);
556 void visitStoreInst(StoreInst &SI);
557 void verifyDominatesUse(Instruction &I, unsigned i);
558 void visitInstruction(Instruction &I);
559 void visitTerminator(Instruction &I);
560 void visitBranchInst(BranchInst &BI);
561 void visitReturnInst(ReturnInst &RI);
562 void visitSwitchInst(SwitchInst &SI);
563 void visitIndirectBrInst(IndirectBrInst &BI);
564 void visitCallBrInst(CallBrInst &CBI);
565 void visitSelectInst(SelectInst &SI);
566 void visitUserOp1(Instruction &I);
visitUserOp2(Instruction & I)567 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
568 void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
569 void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
570 void visitVPIntrinsic(VPIntrinsic &VPI);
571 void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
572 void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
573 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
574 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
575 void visitFenceInst(FenceInst &FI);
576 void visitAllocaInst(AllocaInst &AI);
577 void visitExtractValueInst(ExtractValueInst &EVI);
578 void visitInsertValueInst(InsertValueInst &IVI);
579 void visitEHPadPredecessors(Instruction &I);
580 void visitLandingPadInst(LandingPadInst &LPI);
581 void visitResumeInst(ResumeInst &RI);
582 void visitCatchPadInst(CatchPadInst &CPI);
583 void visitCatchReturnInst(CatchReturnInst &CatchReturn);
584 void visitCleanupPadInst(CleanupPadInst &CPI);
585 void visitFuncletPadInst(FuncletPadInst &FPI);
586 void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
587 void visitCleanupReturnInst(CleanupReturnInst &CRI);
588
589 void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
590 void verifySwiftErrorValue(const Value *SwiftErrorVal);
591 void verifyTailCCMustTailAttrs(const AttrBuilder &Attrs, StringRef Context);
592 void verifyMustTailCall(CallInst &CI);
593 bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
594 void verifyAttributeTypes(AttributeSet Attrs, const Value *V);
595 void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
596 void checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
597 const Value *V);
598 void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
599 const Value *V, bool IsIntrinsic, bool IsInlineAsm);
600 void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
601
602 void visitConstantExprsRecursively(const Constant *EntryC);
603 void visitConstantExpr(const ConstantExpr *CE);
604 void verifyInlineAsmCall(const CallBase &Call);
605 void verifyStatepoint(const CallBase &Call);
606 void verifyFrameRecoverIndices();
607 void verifySiblingFuncletUnwinds();
608
609 void verifyFragmentExpression(const DbgVariableIntrinsic &I);
610 template <typename ValueOrMetadata>
611 void verifyFragmentExpression(const DIVariable &V,
612 DIExpression::FragmentInfo Fragment,
613 ValueOrMetadata *Desc);
614 void verifyFnArgs(const DbgVariableIntrinsic &I);
615 void verifyNotEntryValue(const DbgVariableIntrinsic &I);
616
617 /// Module-level debug info verification...
618 void verifyCompileUnits();
619
620 /// Module-level verification that all @llvm.experimental.deoptimize
621 /// declarations share the same calling convention.
622 void verifyDeoptimizeCallingConvs();
623
624 void verifyAttachedCallBundle(const CallBase &Call,
625 const OperandBundleUse &BU);
626
627 /// Verify the llvm.experimental.noalias.scope.decl declarations
628 void verifyNoAliasScopeDecl();
629 };
630
631 } // end anonymous namespace
632
633 /// We know that cond should be true, if not print an error message.
634 #define Check(C, ...) \
635 do { \
636 if (!(C)) { \
637 CheckFailed(__VA_ARGS__); \
638 return; \
639 } \
640 } while (false)
641
642 /// We know that a debug info condition should be true, if not print
643 /// an error message.
644 #define CheckDI(C, ...) \
645 do { \
646 if (!(C)) { \
647 DebugInfoCheckFailed(__VA_ARGS__); \
648 return; \
649 } \
650 } while (false)
651
visit(Instruction & I)652 void Verifier::visit(Instruction &I) {
653 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
654 Check(I.getOperand(i) != nullptr, "Operand is null", &I);
655 InstVisitor<Verifier>::visit(I);
656 }
657
658 // Helper to iterate over indirect users. By returning false, the callback can ask to stop traversing further.
forEachUser(const Value * User,SmallPtrSet<const Value *,32> & Visited,llvm::function_ref<bool (const Value *)> Callback)659 static void forEachUser(const Value *User,
660 SmallPtrSet<const Value *, 32> &Visited,
661 llvm::function_ref<bool(const Value *)> Callback) {
662 if (!Visited.insert(User).second)
663 return;
664
665 SmallVector<const Value *> WorkList;
666 append_range(WorkList, User->materialized_users());
667 while (!WorkList.empty()) {
668 const Value *Cur = WorkList.pop_back_val();
669 if (!Visited.insert(Cur).second)
670 continue;
671 if (Callback(Cur))
672 append_range(WorkList, Cur->materialized_users());
673 }
674 }
675
visitGlobalValue(const GlobalValue & GV)676 void Verifier::visitGlobalValue(const GlobalValue &GV) {
677 Check(!GV.isDeclaration() || GV.hasValidDeclarationLinkage(),
678 "Global is external, but doesn't have external or weak linkage!", &GV);
679
680 if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) {
681
682 if (MaybeAlign A = GO->getAlign()) {
683 Check(A->value() <= Value::MaximumAlignment,
684 "huge alignment values are unsupported", GO);
685 }
686
687 if (const MDNode *Associated =
688 GO->getMetadata(LLVMContext::MD_associated)) {
689 Check(Associated->getNumOperands() == 1,
690 "associated metadata must have one operand", &GV, Associated);
691 const Metadata *Op = Associated->getOperand(0).get();
692 Check(Op, "associated metadata must have a global value", GO, Associated);
693
694 const auto *VM = dyn_cast_or_null<ValueAsMetadata>(Op);
695 Check(VM, "associated metadata must be ValueAsMetadata", GO, Associated);
696 if (VM) {
697 Check(isa<PointerType>(VM->getValue()->getType()),
698 "associated value must be pointer typed", GV, Associated);
699
700 const Value *Stripped = VM->getValue()->stripPointerCastsAndAliases();
701 Check(isa<GlobalObject>(Stripped) || isa<Constant>(Stripped),
702 "associated metadata must point to a GlobalObject", GO, Stripped);
703 Check(Stripped != GO,
704 "global values should not associate to themselves", GO,
705 Associated);
706 }
707 }
708
709 // FIXME: Why is getMetadata on GlobalValue protected?
710 if (const MDNode *AbsoluteSymbol =
711 GO->getMetadata(LLVMContext::MD_absolute_symbol)) {
712 verifyRangeMetadata(*GO, AbsoluteSymbol, DL.getIntPtrType(GO->getType()),
713 true);
714 }
715 }
716
717 Check(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
718 "Only global variables can have appending linkage!", &GV);
719
720 if (GV.hasAppendingLinkage()) {
721 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
722 Check(GVar && GVar->getValueType()->isArrayTy(),
723 "Only global arrays can have appending linkage!", GVar);
724 }
725
726 if (GV.isDeclarationForLinker())
727 Check(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
728
729 if (GV.hasDLLExportStorageClass()) {
730 Check(!GV.hasHiddenVisibility(),
731 "dllexport GlobalValue must have default or protected visibility",
732 &GV);
733 }
734 if (GV.hasDLLImportStorageClass()) {
735 Check(GV.hasDefaultVisibility(),
736 "dllimport GlobalValue must have default visibility", &GV);
737 Check(!GV.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!",
738 &GV);
739
740 Check((GV.isDeclaration() &&
741 (GV.hasExternalLinkage() || GV.hasExternalWeakLinkage())) ||
742 GV.hasAvailableExternallyLinkage(),
743 "Global is marked as dllimport, but not external", &GV);
744 }
745
746 if (GV.isImplicitDSOLocal())
747 Check(GV.isDSOLocal(),
748 "GlobalValue with local linkage or non-default "
749 "visibility must be dso_local!",
750 &GV);
751
752 forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
753 if (const Instruction *I = dyn_cast<Instruction>(V)) {
754 if (!I->getParent() || !I->getParent()->getParent())
755 CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
756 I);
757 else if (I->getParent()->getParent()->getParent() != &M)
758 CheckFailed("Global is referenced in a different module!", &GV, &M, I,
759 I->getParent()->getParent(),
760 I->getParent()->getParent()->getParent());
761 return false;
762 } else if (const Function *F = dyn_cast<Function>(V)) {
763 if (F->getParent() != &M)
764 CheckFailed("Global is used by function in a different module", &GV, &M,
765 F, F->getParent());
766 return false;
767 }
768 return true;
769 });
770 }
771
visitGlobalVariable(const GlobalVariable & GV)772 void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
773 if (GV.hasInitializer()) {
774 Check(GV.getInitializer()->getType() == GV.getValueType(),
775 "Global variable initializer type does not match global "
776 "variable type!",
777 &GV);
778 // If the global has common linkage, it must have a zero initializer and
779 // cannot be constant.
780 if (GV.hasCommonLinkage()) {
781 Check(GV.getInitializer()->isNullValue(),
782 "'common' global must have a zero initializer!", &GV);
783 Check(!GV.isConstant(), "'common' global may not be marked constant!",
784 &GV);
785 Check(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
786 }
787 }
788
789 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
790 GV.getName() == "llvm.global_dtors")) {
791 Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
792 "invalid linkage for intrinsic global variable", &GV);
793 Check(GV.materialized_use_empty(),
794 "invalid uses of intrinsic global variable", &GV);
795
796 // Don't worry about emitting an error for it not being an array,
797 // visitGlobalValue will complain on appending non-array.
798 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
799 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
800 PointerType *FuncPtrTy =
801 PointerType::get(Context, DL.getProgramAddressSpace());
802 Check(STy && (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
803 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
804 STy->getTypeAtIndex(1) == FuncPtrTy,
805 "wrong type for intrinsic global variable", &GV);
806 Check(STy->getNumElements() == 3,
807 "the third field of the element type is mandatory, "
808 "specify ptr null to migrate from the obsoleted 2-field form");
809 Type *ETy = STy->getTypeAtIndex(2);
810 Check(ETy->isPointerTy(), "wrong type for intrinsic global variable",
811 &GV);
812 }
813 }
814
815 if (GV.hasName() && (GV.getName() == "llvm.used" ||
816 GV.getName() == "llvm.compiler.used")) {
817 Check(!GV.hasInitializer() || GV.hasAppendingLinkage(),
818 "invalid linkage for intrinsic global variable", &GV);
819 Check(GV.materialized_use_empty(),
820 "invalid uses of intrinsic global variable", &GV);
821
822 Type *GVType = GV.getValueType();
823 if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
824 PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
825 Check(PTy, "wrong type for intrinsic global variable", &GV);
826 if (GV.hasInitializer()) {
827 const Constant *Init = GV.getInitializer();
828 const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
829 Check(InitArray, "wrong initalizer for intrinsic global variable",
830 Init);
831 for (Value *Op : InitArray->operands()) {
832 Value *V = Op->stripPointerCasts();
833 Check(isa<GlobalVariable>(V) || isa<Function>(V) ||
834 isa<GlobalAlias>(V),
835 Twine("invalid ") + GV.getName() + " member", V);
836 Check(V->hasName(),
837 Twine("members of ") + GV.getName() + " must be named", V);
838 }
839 }
840 }
841 }
842
843 // Visit any debug info attachments.
844 SmallVector<MDNode *, 1> MDs;
845 GV.getMetadata(LLVMContext::MD_dbg, MDs);
846 for (auto *MD : MDs) {
847 if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
848 visitDIGlobalVariableExpression(*GVE);
849 else
850 CheckDI(false, "!dbg attachment of global variable must be a "
851 "DIGlobalVariableExpression");
852 }
853
854 // Scalable vectors cannot be global variables, since we don't know
855 // the runtime size.
856 Check(!GV.getValueType()->isScalableTy(),
857 "Globals cannot contain scalable types", &GV);
858
859 // Check if it's a target extension type that disallows being used as a
860 // global.
861 if (auto *TTy = dyn_cast<TargetExtType>(GV.getValueType()))
862 Check(TTy->hasProperty(TargetExtType::CanBeGlobal),
863 "Global @" + GV.getName() + " has illegal target extension type",
864 TTy);
865
866 if (!GV.hasInitializer()) {
867 visitGlobalValue(GV);
868 return;
869 }
870
871 // Walk any aggregate initializers looking for bitcasts between address spaces
872 visitConstantExprsRecursively(GV.getInitializer());
873
874 visitGlobalValue(GV);
875 }
876
visitAliaseeSubExpr(const GlobalAlias & GA,const Constant & C)877 void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
878 SmallPtrSet<const GlobalAlias*, 4> Visited;
879 Visited.insert(&GA);
880 visitAliaseeSubExpr(Visited, GA, C);
881 }
882
visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias * > & Visited,const GlobalAlias & GA,const Constant & C)883 void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
884 const GlobalAlias &GA, const Constant &C) {
885 if (GA.hasAvailableExternallyLinkage()) {
886 Check(isa<GlobalValue>(C) &&
887 cast<GlobalValue>(C).hasAvailableExternallyLinkage(),
888 "available_externally alias must point to available_externally "
889 "global value",
890 &GA);
891 }
892 if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
893 if (!GA.hasAvailableExternallyLinkage()) {
894 Check(!GV->isDeclarationForLinker(), "Alias must point to a definition",
895 &GA);
896 }
897
898 if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
899 Check(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
900
901 Check(!GA2->isInterposable(),
902 "Alias cannot point to an interposable alias", &GA);
903 } else {
904 // Only continue verifying subexpressions of GlobalAliases.
905 // Do not recurse into global initializers.
906 return;
907 }
908 }
909
910 if (const auto *CE = dyn_cast<ConstantExpr>(&C))
911 visitConstantExprsRecursively(CE);
912
913 for (const Use &U : C.operands()) {
914 Value *V = &*U;
915 if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
916 visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
917 else if (const auto *C2 = dyn_cast<Constant>(V))
918 visitAliaseeSubExpr(Visited, GA, *C2);
919 }
920 }
921
visitGlobalAlias(const GlobalAlias & GA)922 void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
923 Check(GlobalAlias::isValidLinkage(GA.getLinkage()),
924 "Alias should have private, internal, linkonce, weak, linkonce_odr, "
925 "weak_odr, external, or available_externally linkage!",
926 &GA);
927 const Constant *Aliasee = GA.getAliasee();
928 Check(Aliasee, "Aliasee cannot be NULL!", &GA);
929 Check(GA.getType() == Aliasee->getType(),
930 "Alias and aliasee types should match!", &GA);
931
932 Check(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
933 "Aliasee should be either GlobalValue or ConstantExpr", &GA);
934
935 visitAliaseeSubExpr(GA, *Aliasee);
936
937 visitGlobalValue(GA);
938 }
939
visitGlobalIFunc(const GlobalIFunc & GI)940 void Verifier::visitGlobalIFunc(const GlobalIFunc &GI) {
941 Check(GlobalIFunc::isValidLinkage(GI.getLinkage()),
942 "IFunc should have private, internal, linkonce, weak, linkonce_odr, "
943 "weak_odr, or external linkage!",
944 &GI);
945 // Pierce through ConstantExprs and GlobalAliases and check that the resolver
946 // is a Function definition.
947 const Function *Resolver = GI.getResolverFunction();
948 Check(Resolver, "IFunc must have a Function resolver", &GI);
949 Check(!Resolver->isDeclarationForLinker(),
950 "IFunc resolver must be a definition", &GI);
951
952 // Check that the immediate resolver operand (prior to any bitcasts) has the
953 // correct type.
954 const Type *ResolverTy = GI.getResolver()->getType();
955
956 Check(isa<PointerType>(Resolver->getFunctionType()->getReturnType()),
957 "IFunc resolver must return a pointer", &GI);
958
959 const Type *ResolverFuncTy =
960 GlobalIFunc::getResolverFunctionType(GI.getValueType());
961 Check(ResolverTy == ResolverFuncTy->getPointerTo(GI.getAddressSpace()),
962 "IFunc resolver has incorrect type", &GI);
963 }
964
visitNamedMDNode(const NamedMDNode & NMD)965 void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
966 // There used to be various other llvm.dbg.* nodes, but we don't support
967 // upgrading them and we want to reserve the namespace for future uses.
968 if (NMD.getName().starts_with("llvm.dbg."))
969 CheckDI(NMD.getName() == "llvm.dbg.cu",
970 "unrecognized named metadata node in the llvm.dbg namespace", &NMD);
971 for (const MDNode *MD : NMD.operands()) {
972 if (NMD.getName() == "llvm.dbg.cu")
973 CheckDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
974
975 if (!MD)
976 continue;
977
978 visitMDNode(*MD, AreDebugLocsAllowed::Yes);
979 }
980 }
981
visitMDNode(const MDNode & MD,AreDebugLocsAllowed AllowLocs)982 void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
983 // Only visit each node once. Metadata can be mutually recursive, so this
984 // avoids infinite recursion here, as well as being an optimization.
985 if (!MDNodes.insert(&MD).second)
986 return;
987
988 Check(&MD.getContext() == &Context,
989 "MDNode context does not match Module context!", &MD);
990
991 switch (MD.getMetadataID()) {
992 default:
993 llvm_unreachable("Invalid MDNode subclass");
994 case Metadata::MDTupleKind:
995 break;
996 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
997 case Metadata::CLASS##Kind: \
998 visit##CLASS(cast<CLASS>(MD)); \
999 break;
1000 #include "llvm/IR/Metadata.def"
1001 }
1002
1003 for (const Metadata *Op : MD.operands()) {
1004 if (!Op)
1005 continue;
1006 Check(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
1007 &MD, Op);
1008 CheckDI(!isa<DILocation>(Op) || AllowLocs == AreDebugLocsAllowed::Yes,
1009 "DILocation not allowed within this metadata node", &MD, Op);
1010 if (auto *N = dyn_cast<MDNode>(Op)) {
1011 visitMDNode(*N, AllowLocs);
1012 continue;
1013 }
1014 if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
1015 visitValueAsMetadata(*V, nullptr);
1016 continue;
1017 }
1018 }
1019
1020 // Check these last, so we diagnose problems in operands first.
1021 Check(!MD.isTemporary(), "Expected no forward declarations!", &MD);
1022 Check(MD.isResolved(), "All nodes should be resolved!", &MD);
1023 }
1024
visitValueAsMetadata(const ValueAsMetadata & MD,Function * F)1025 void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
1026 Check(MD.getValue(), "Expected valid value", &MD);
1027 Check(!MD.getValue()->getType()->isMetadataTy(),
1028 "Unexpected metadata round-trip through values", &MD, MD.getValue());
1029
1030 auto *L = dyn_cast<LocalAsMetadata>(&MD);
1031 if (!L)
1032 return;
1033
1034 Check(F, "function-local metadata used outside a function", L);
1035
1036 // If this was an instruction, bb, or argument, verify that it is in the
1037 // function that we expect.
1038 Function *ActualF = nullptr;
1039 if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
1040 Check(I->getParent(), "function-local metadata not in basic block", L, I);
1041 ActualF = I->getParent()->getParent();
1042 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
1043 ActualF = BB->getParent();
1044 else if (Argument *A = dyn_cast<Argument>(L->getValue()))
1045 ActualF = A->getParent();
1046 assert(ActualF && "Unimplemented function local metadata case!");
1047
1048 Check(ActualF == F, "function-local metadata used in wrong function", L);
1049 }
1050
visitDIArgList(const DIArgList & AL,Function * F)1051 void Verifier::visitDIArgList(const DIArgList &AL, Function *F) {
1052 for (const ValueAsMetadata *VAM : AL.getArgs())
1053 visitValueAsMetadata(*VAM, F);
1054 }
1055
visitMetadataAsValue(const MetadataAsValue & MDV,Function * F)1056 void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
1057 Metadata *MD = MDV.getMetadata();
1058 if (auto *N = dyn_cast<MDNode>(MD)) {
1059 visitMDNode(*N, AreDebugLocsAllowed::No);
1060 return;
1061 }
1062
1063 // Only visit each node once. Metadata can be mutually recursive, so this
1064 // avoids infinite recursion here, as well as being an optimization.
1065 if (!MDNodes.insert(MD).second)
1066 return;
1067
1068 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
1069 visitValueAsMetadata(*V, F);
1070
1071 if (auto *AL = dyn_cast<DIArgList>(MD))
1072 visitDIArgList(*AL, F);
1073 }
1074
isType(const Metadata * MD)1075 static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
isScope(const Metadata * MD)1076 static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
isDINode(const Metadata * MD)1077 static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
1078
visitDILocation(const DILocation & N)1079 void Verifier::visitDILocation(const DILocation &N) {
1080 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1081 "location requires a valid scope", &N, N.getRawScope());
1082 if (auto *IA = N.getRawInlinedAt())
1083 CheckDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
1084 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1085 CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1086 }
1087
visitGenericDINode(const GenericDINode & N)1088 void Verifier::visitGenericDINode(const GenericDINode &N) {
1089 CheckDI(N.getTag(), "invalid tag", &N);
1090 }
1091
visitDIScope(const DIScope & N)1092 void Verifier::visitDIScope(const DIScope &N) {
1093 if (auto *F = N.getRawFile())
1094 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1095 }
1096
visitDISubrange(const DISubrange & N)1097 void Verifier::visitDISubrange(const DISubrange &N) {
1098 CheckDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
1099 bool HasAssumedSizedArraySupport = dwarf::isFortran(CurrentSourceLang);
1100 CheckDI(HasAssumedSizedArraySupport || N.getRawCountNode() ||
1101 N.getRawUpperBound(),
1102 "Subrange must contain count or upperBound", &N);
1103 CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(),
1104 "Subrange can have any one of count or upperBound", &N);
1105 auto *CBound = N.getRawCountNode();
1106 CheckDI(!CBound || isa<ConstantAsMetadata>(CBound) ||
1107 isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
1108 "Count must be signed constant or DIVariable or DIExpression", &N);
1109 auto Count = N.getCount();
1110 CheckDI(!Count || !isa<ConstantInt *>(Count) ||
1111 cast<ConstantInt *>(Count)->getSExtValue() >= -1,
1112 "invalid subrange count", &N);
1113 auto *LBound = N.getRawLowerBound();
1114 CheckDI(!LBound || isa<ConstantAsMetadata>(LBound) ||
1115 isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
1116 "LowerBound must be signed constant or DIVariable or DIExpression",
1117 &N);
1118 auto *UBound = N.getRawUpperBound();
1119 CheckDI(!UBound || isa<ConstantAsMetadata>(UBound) ||
1120 isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
1121 "UpperBound must be signed constant or DIVariable or DIExpression",
1122 &N);
1123 auto *Stride = N.getRawStride();
1124 CheckDI(!Stride || isa<ConstantAsMetadata>(Stride) ||
1125 isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
1126 "Stride must be signed constant or DIVariable or DIExpression", &N);
1127 }
1128
visitDIGenericSubrange(const DIGenericSubrange & N)1129 void Verifier::visitDIGenericSubrange(const DIGenericSubrange &N) {
1130 CheckDI(N.getTag() == dwarf::DW_TAG_generic_subrange, "invalid tag", &N);
1131 CheckDI(N.getRawCountNode() || N.getRawUpperBound(),
1132 "GenericSubrange must contain count or upperBound", &N);
1133 CheckDI(!N.getRawCountNode() || !N.getRawUpperBound(),
1134 "GenericSubrange can have any one of count or upperBound", &N);
1135 auto *CBound = N.getRawCountNode();
1136 CheckDI(!CBound || isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
1137 "Count must be signed constant or DIVariable or DIExpression", &N);
1138 auto *LBound = N.getRawLowerBound();
1139 CheckDI(LBound, "GenericSubrange must contain lowerBound", &N);
1140 CheckDI(isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
1141 "LowerBound must be signed constant or DIVariable or DIExpression",
1142 &N);
1143 auto *UBound = N.getRawUpperBound();
1144 CheckDI(!UBound || isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
1145 "UpperBound must be signed constant or DIVariable or DIExpression",
1146 &N);
1147 auto *Stride = N.getRawStride();
1148 CheckDI(Stride, "GenericSubrange must contain stride", &N);
1149 CheckDI(isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
1150 "Stride must be signed constant or DIVariable or DIExpression", &N);
1151 }
1152
visitDIEnumerator(const DIEnumerator & N)1153 void Verifier::visitDIEnumerator(const DIEnumerator &N) {
1154 CheckDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
1155 }
1156
visitDIBasicType(const DIBasicType & N)1157 void Verifier::visitDIBasicType(const DIBasicType &N) {
1158 CheckDI(N.getTag() == dwarf::DW_TAG_base_type ||
1159 N.getTag() == dwarf::DW_TAG_unspecified_type ||
1160 N.getTag() == dwarf::DW_TAG_string_type,
1161 "invalid tag", &N);
1162 }
1163
visitDIStringType(const DIStringType & N)1164 void Verifier::visitDIStringType(const DIStringType &N) {
1165 CheckDI(N.getTag() == dwarf::DW_TAG_string_type, "invalid tag", &N);
1166 CheckDI(!(N.isBigEndian() && N.isLittleEndian()), "has conflicting flags",
1167 &N);
1168 }
1169
visitDIDerivedType(const DIDerivedType & N)1170 void Verifier::visitDIDerivedType(const DIDerivedType &N) {
1171 // Common scope checks.
1172 visitDIScope(N);
1173
1174 CheckDI(N.getTag() == dwarf::DW_TAG_typedef ||
1175 N.getTag() == dwarf::DW_TAG_pointer_type ||
1176 N.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
1177 N.getTag() == dwarf::DW_TAG_reference_type ||
1178 N.getTag() == dwarf::DW_TAG_rvalue_reference_type ||
1179 N.getTag() == dwarf::DW_TAG_const_type ||
1180 N.getTag() == dwarf::DW_TAG_immutable_type ||
1181 N.getTag() == dwarf::DW_TAG_volatile_type ||
1182 N.getTag() == dwarf::DW_TAG_restrict_type ||
1183 N.getTag() == dwarf::DW_TAG_atomic_type ||
1184 N.getTag() == dwarf::DW_TAG_member ||
1185 (N.getTag() == dwarf::DW_TAG_variable && N.isStaticMember()) ||
1186 N.getTag() == dwarf::DW_TAG_inheritance ||
1187 N.getTag() == dwarf::DW_TAG_friend ||
1188 N.getTag() == dwarf::DW_TAG_set_type,
1189 "invalid tag", &N);
1190 if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
1191 CheckDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
1192 N.getRawExtraData());
1193 }
1194
1195 if (N.getTag() == dwarf::DW_TAG_set_type) {
1196 if (auto *T = N.getRawBaseType()) {
1197 auto *Enum = dyn_cast_or_null<DICompositeType>(T);
1198 auto *Basic = dyn_cast_or_null<DIBasicType>(T);
1199 CheckDI(
1200 (Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type) ||
1201 (Basic && (Basic->getEncoding() == dwarf::DW_ATE_unsigned ||
1202 Basic->getEncoding() == dwarf::DW_ATE_signed ||
1203 Basic->getEncoding() == dwarf::DW_ATE_unsigned_char ||
1204 Basic->getEncoding() == dwarf::DW_ATE_signed_char ||
1205 Basic->getEncoding() == dwarf::DW_ATE_boolean)),
1206 "invalid set base type", &N, T);
1207 }
1208 }
1209
1210 CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1211 CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
1212 N.getRawBaseType());
1213
1214 if (N.getDWARFAddressSpace()) {
1215 CheckDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
1216 N.getTag() == dwarf::DW_TAG_reference_type ||
1217 N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
1218 "DWARF address space only applies to pointer or reference types",
1219 &N);
1220 }
1221 }
1222
1223 /// Detect mutually exclusive flags.
hasConflictingReferenceFlags(unsigned Flags)1224 static bool hasConflictingReferenceFlags(unsigned Flags) {
1225 return ((Flags & DINode::FlagLValueReference) &&
1226 (Flags & DINode::FlagRValueReference)) ||
1227 ((Flags & DINode::FlagTypePassByValue) &&
1228 (Flags & DINode::FlagTypePassByReference));
1229 }
1230
visitTemplateParams(const MDNode & N,const Metadata & RawParams)1231 void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
1232 auto *Params = dyn_cast<MDTuple>(&RawParams);
1233 CheckDI(Params, "invalid template params", &N, &RawParams);
1234 for (Metadata *Op : Params->operands()) {
1235 CheckDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
1236 &N, Params, Op);
1237 }
1238 }
1239
visitDICompositeType(const DICompositeType & N)1240 void Verifier::visitDICompositeType(const DICompositeType &N) {
1241 // Common scope checks.
1242 visitDIScope(N);
1243
1244 CheckDI(N.getTag() == dwarf::DW_TAG_array_type ||
1245 N.getTag() == dwarf::DW_TAG_structure_type ||
1246 N.getTag() == dwarf::DW_TAG_union_type ||
1247 N.getTag() == dwarf::DW_TAG_enumeration_type ||
1248 N.getTag() == dwarf::DW_TAG_class_type ||
1249 N.getTag() == dwarf::DW_TAG_variant_part ||
1250 N.getTag() == dwarf::DW_TAG_namelist,
1251 "invalid tag", &N);
1252
1253 CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1254 CheckDI(isType(N.getRawBaseType()), "invalid base type", &N,
1255 N.getRawBaseType());
1256
1257 CheckDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
1258 "invalid composite elements", &N, N.getRawElements());
1259 CheckDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
1260 N.getRawVTableHolder());
1261 CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
1262 "invalid reference flags", &N);
1263 unsigned DIBlockByRefStruct = 1 << 4;
1264 CheckDI((N.getFlags() & DIBlockByRefStruct) == 0,
1265 "DIBlockByRefStruct on DICompositeType is no longer supported", &N);
1266
1267 if (N.isVector()) {
1268 const DINodeArray Elements = N.getElements();
1269 CheckDI(Elements.size() == 1 &&
1270 Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
1271 "invalid vector, expected one element of type subrange", &N);
1272 }
1273
1274 if (auto *Params = N.getRawTemplateParams())
1275 visitTemplateParams(N, *Params);
1276
1277 if (auto *D = N.getRawDiscriminator()) {
1278 CheckDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
1279 "discriminator can only appear on variant part");
1280 }
1281
1282 if (N.getRawDataLocation()) {
1283 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1284 "dataLocation can only appear in array type");
1285 }
1286
1287 if (N.getRawAssociated()) {
1288 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1289 "associated can only appear in array type");
1290 }
1291
1292 if (N.getRawAllocated()) {
1293 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1294 "allocated can only appear in array type");
1295 }
1296
1297 if (N.getRawRank()) {
1298 CheckDI(N.getTag() == dwarf::DW_TAG_array_type,
1299 "rank can only appear in array type");
1300 }
1301
1302 if (N.getTag() == dwarf::DW_TAG_array_type) {
1303 CheckDI(N.getRawBaseType(), "array types must have a base type", &N);
1304 }
1305 }
1306
visitDISubroutineType(const DISubroutineType & N)1307 void Verifier::visitDISubroutineType(const DISubroutineType &N) {
1308 CheckDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
1309 if (auto *Types = N.getRawTypeArray()) {
1310 CheckDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
1311 for (Metadata *Ty : N.getTypeArray()->operands()) {
1312 CheckDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
1313 }
1314 }
1315 CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
1316 "invalid reference flags", &N);
1317 }
1318
visitDIFile(const DIFile & N)1319 void Verifier::visitDIFile(const DIFile &N) {
1320 CheckDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
1321 std::optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
1322 if (Checksum) {
1323 CheckDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
1324 "invalid checksum kind", &N);
1325 size_t Size;
1326 switch (Checksum->Kind) {
1327 case DIFile::CSK_MD5:
1328 Size = 32;
1329 break;
1330 case DIFile::CSK_SHA1:
1331 Size = 40;
1332 break;
1333 case DIFile::CSK_SHA256:
1334 Size = 64;
1335 break;
1336 }
1337 CheckDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
1338 CheckDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
1339 "invalid checksum", &N);
1340 }
1341 }
1342
visitDICompileUnit(const DICompileUnit & N)1343 void Verifier::visitDICompileUnit(const DICompileUnit &N) {
1344 CheckDI(N.isDistinct(), "compile units must be distinct", &N);
1345 CheckDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
1346
1347 // Don't bother verifying the compilation directory or producer string
1348 // as those could be empty.
1349 CheckDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
1350 N.getRawFile());
1351 CheckDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
1352 N.getFile());
1353
1354 CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage();
1355
1356 CheckDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
1357 "invalid emission kind", &N);
1358
1359 if (auto *Array = N.getRawEnumTypes()) {
1360 CheckDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
1361 for (Metadata *Op : N.getEnumTypes()->operands()) {
1362 auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
1363 CheckDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
1364 "invalid enum type", &N, N.getEnumTypes(), Op);
1365 }
1366 }
1367 if (auto *Array = N.getRawRetainedTypes()) {
1368 CheckDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
1369 for (Metadata *Op : N.getRetainedTypes()->operands()) {
1370 CheckDI(
1371 Op && (isa<DIType>(Op) || (isa<DISubprogram>(Op) &&
1372 !cast<DISubprogram>(Op)->isDefinition())),
1373 "invalid retained type", &N, Op);
1374 }
1375 }
1376 if (auto *Array = N.getRawGlobalVariables()) {
1377 CheckDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
1378 for (Metadata *Op : N.getGlobalVariables()->operands()) {
1379 CheckDI(Op && (isa<DIGlobalVariableExpression>(Op)),
1380 "invalid global variable ref", &N, Op);
1381 }
1382 }
1383 if (auto *Array = N.getRawImportedEntities()) {
1384 CheckDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
1385 for (Metadata *Op : N.getImportedEntities()->operands()) {
1386 CheckDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
1387 &N, Op);
1388 }
1389 }
1390 if (auto *Array = N.getRawMacros()) {
1391 CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1392 for (Metadata *Op : N.getMacros()->operands()) {
1393 CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1394 }
1395 }
1396 CUVisited.insert(&N);
1397 }
1398
visitDISubprogram(const DISubprogram & N)1399 void Verifier::visitDISubprogram(const DISubprogram &N) {
1400 CheckDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
1401 CheckDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1402 if (auto *F = N.getRawFile())
1403 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1404 else
1405 CheckDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
1406 if (auto *T = N.getRawType())
1407 CheckDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
1408 CheckDI(isType(N.getRawContainingType()), "invalid containing type", &N,
1409 N.getRawContainingType());
1410 if (auto *Params = N.getRawTemplateParams())
1411 visitTemplateParams(N, *Params);
1412 if (auto *S = N.getRawDeclaration())
1413 CheckDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
1414 "invalid subprogram declaration", &N, S);
1415 if (auto *RawNode = N.getRawRetainedNodes()) {
1416 auto *Node = dyn_cast<MDTuple>(RawNode);
1417 CheckDI(Node, "invalid retained nodes list", &N, RawNode);
1418 for (Metadata *Op : Node->operands()) {
1419 CheckDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op) ||
1420 isa<DIImportedEntity>(Op)),
1421 "invalid retained nodes, expected DILocalVariable, DILabel or "
1422 "DIImportedEntity",
1423 &N, Node, Op);
1424 }
1425 }
1426 CheckDI(!hasConflictingReferenceFlags(N.getFlags()),
1427 "invalid reference flags", &N);
1428
1429 auto *Unit = N.getRawUnit();
1430 if (N.isDefinition()) {
1431 // Subprogram definitions (not part of the type hierarchy).
1432 CheckDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
1433 CheckDI(Unit, "subprogram definitions must have a compile unit", &N);
1434 CheckDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
1435 // There's no good way to cross the CU boundary to insert a nested
1436 // DISubprogram definition in one CU into a type defined in another CU.
1437 auto *CT = dyn_cast_or_null<DICompositeType>(N.getRawScope());
1438 if (CT && CT->getRawIdentifier() &&
1439 M.getContext().isODRUniquingDebugTypes())
1440 CheckDI(N.getDeclaration(),
1441 "definition subprograms cannot be nested within DICompositeType "
1442 "when enabling ODR",
1443 &N);
1444 } else {
1445 // Subprogram declarations (part of the type hierarchy).
1446 CheckDI(!Unit, "subprogram declarations must not have a compile unit", &N);
1447 CheckDI(!N.getRawDeclaration(),
1448 "subprogram declaration must not have a declaration field");
1449 }
1450
1451 if (auto *RawThrownTypes = N.getRawThrownTypes()) {
1452 auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
1453 CheckDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
1454 for (Metadata *Op : ThrownTypes->operands())
1455 CheckDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
1456 Op);
1457 }
1458
1459 if (N.areAllCallsDescribed())
1460 CheckDI(N.isDefinition(),
1461 "DIFlagAllCallsDescribed must be attached to a definition");
1462 }
1463
visitDILexicalBlockBase(const DILexicalBlockBase & N)1464 void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
1465 CheckDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
1466 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1467 "invalid local scope", &N, N.getRawScope());
1468 if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1469 CheckDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1470 }
1471
visitDILexicalBlock(const DILexicalBlock & N)1472 void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
1473 visitDILexicalBlockBase(N);
1474
1475 CheckDI(N.getLine() || !N.getColumn(),
1476 "cannot have column info without line info", &N);
1477 }
1478
visitDILexicalBlockFile(const DILexicalBlockFile & N)1479 void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
1480 visitDILexicalBlockBase(N);
1481 }
1482
visitDICommonBlock(const DICommonBlock & N)1483 void Verifier::visitDICommonBlock(const DICommonBlock &N) {
1484 CheckDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
1485 if (auto *S = N.getRawScope())
1486 CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1487 if (auto *S = N.getRawDecl())
1488 CheckDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
1489 }
1490
visitDINamespace(const DINamespace & N)1491 void Verifier::visitDINamespace(const DINamespace &N) {
1492 CheckDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
1493 if (auto *S = N.getRawScope())
1494 CheckDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1495 }
1496
visitDIMacro(const DIMacro & N)1497 void Verifier::visitDIMacro(const DIMacro &N) {
1498 CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
1499 N.getMacinfoType() == dwarf::DW_MACINFO_undef,
1500 "invalid macinfo type", &N);
1501 CheckDI(!N.getName().empty(), "anonymous macro", &N);
1502 if (!N.getValue().empty()) {
1503 assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
1504 }
1505 }
1506
visitDIMacroFile(const DIMacroFile & N)1507 void Verifier::visitDIMacroFile(const DIMacroFile &N) {
1508 CheckDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
1509 "invalid macinfo type", &N);
1510 if (auto *F = N.getRawFile())
1511 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1512
1513 if (auto *Array = N.getRawElements()) {
1514 CheckDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1515 for (Metadata *Op : N.getElements()->operands()) {
1516 CheckDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1517 }
1518 }
1519 }
1520
visitDIModule(const DIModule & N)1521 void Verifier::visitDIModule(const DIModule &N) {
1522 CheckDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
1523 CheckDI(!N.getName().empty(), "anonymous module", &N);
1524 }
1525
visitDITemplateParameter(const DITemplateParameter & N)1526 void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
1527 CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1528 }
1529
visitDITemplateTypeParameter(const DITemplateTypeParameter & N)1530 void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
1531 visitDITemplateParameter(N);
1532
1533 CheckDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
1534 &N);
1535 }
1536
visitDITemplateValueParameter(const DITemplateValueParameter & N)1537 void Verifier::visitDITemplateValueParameter(
1538 const DITemplateValueParameter &N) {
1539 visitDITemplateParameter(N);
1540
1541 CheckDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
1542 N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
1543 N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
1544 "invalid tag", &N);
1545 }
1546
visitDIVariable(const DIVariable & N)1547 void Verifier::visitDIVariable(const DIVariable &N) {
1548 if (auto *S = N.getRawScope())
1549 CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
1550 if (auto *F = N.getRawFile())
1551 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1552 }
1553
visitDIGlobalVariable(const DIGlobalVariable & N)1554 void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
1555 // Checks common to all variables.
1556 visitDIVariable(N);
1557
1558 CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1559 CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1560 // Check only if the global variable is not an extern
1561 if (N.isDefinition())
1562 CheckDI(N.getType(), "missing global variable type", &N);
1563 if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
1564 CheckDI(isa<DIDerivedType>(Member),
1565 "invalid static data member declaration", &N, Member);
1566 }
1567 }
1568
visitDILocalVariable(const DILocalVariable & N)1569 void Verifier::visitDILocalVariable(const DILocalVariable &N) {
1570 // Checks common to all variables.
1571 visitDIVariable(N);
1572
1573 CheckDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1574 CheckDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1575 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1576 "local variable requires a valid scope", &N, N.getRawScope());
1577 if (auto Ty = N.getType())
1578 CheckDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
1579 }
1580
visitDIAssignID(const DIAssignID & N)1581 void Verifier::visitDIAssignID(const DIAssignID &N) {
1582 CheckDI(!N.getNumOperands(), "DIAssignID has no arguments", &N);
1583 CheckDI(N.isDistinct(), "DIAssignID must be distinct", &N);
1584 }
1585
visitDILabel(const DILabel & N)1586 void Verifier::visitDILabel(const DILabel &N) {
1587 if (auto *S = N.getRawScope())
1588 CheckDI(isa<DIScope>(S), "invalid scope", &N, S);
1589 if (auto *F = N.getRawFile())
1590 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1591
1592 CheckDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
1593 CheckDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1594 "label requires a valid scope", &N, N.getRawScope());
1595 }
1596
visitDIExpression(const DIExpression & N)1597 void Verifier::visitDIExpression(const DIExpression &N) {
1598 CheckDI(N.isValid(), "invalid expression", &N);
1599 }
1600
visitDIGlobalVariableExpression(const DIGlobalVariableExpression & GVE)1601 void Verifier::visitDIGlobalVariableExpression(
1602 const DIGlobalVariableExpression &GVE) {
1603 CheckDI(GVE.getVariable(), "missing variable");
1604 if (auto *Var = GVE.getVariable())
1605 visitDIGlobalVariable(*Var);
1606 if (auto *Expr = GVE.getExpression()) {
1607 visitDIExpression(*Expr);
1608 if (auto Fragment = Expr->getFragmentInfo())
1609 verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
1610 }
1611 }
1612
visitDIObjCProperty(const DIObjCProperty & N)1613 void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
1614 CheckDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
1615 if (auto *T = N.getRawType())
1616 CheckDI(isType(T), "invalid type ref", &N, T);
1617 if (auto *F = N.getRawFile())
1618 CheckDI(isa<DIFile>(F), "invalid file", &N, F);
1619 }
1620
visitDIImportedEntity(const DIImportedEntity & N)1621 void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
1622 CheckDI(N.getTag() == dwarf::DW_TAG_imported_module ||
1623 N.getTag() == dwarf::DW_TAG_imported_declaration,
1624 "invalid tag", &N);
1625 if (auto *S = N.getRawScope())
1626 CheckDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
1627 CheckDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
1628 N.getRawEntity());
1629 }
1630
visitComdat(const Comdat & C)1631 void Verifier::visitComdat(const Comdat &C) {
1632 // In COFF the Module is invalid if the GlobalValue has private linkage.
1633 // Entities with private linkage don't have entries in the symbol table.
1634 if (TT.isOSBinFormatCOFF())
1635 if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1636 Check(!GV->hasPrivateLinkage(), "comdat global value has private linkage",
1637 GV);
1638 }
1639
visitModuleIdents()1640 void Verifier::visitModuleIdents() {
1641 const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
1642 if (!Idents)
1643 return;
1644
1645 // llvm.ident takes a list of metadata entry. Each entry has only one string.
1646 // Scan each llvm.ident entry and make sure that this requirement is met.
1647 for (const MDNode *N : Idents->operands()) {
1648 Check(N->getNumOperands() == 1,
1649 "incorrect number of operands in llvm.ident metadata", N);
1650 Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
1651 ("invalid value for llvm.ident metadata entry operand"
1652 "(the operand should be a string)"),
1653 N->getOperand(0));
1654 }
1655 }
1656
visitModuleCommandLines()1657 void Verifier::visitModuleCommandLines() {
1658 const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
1659 if (!CommandLines)
1660 return;
1661
1662 // llvm.commandline takes a list of metadata entry. Each entry has only one
1663 // string. Scan each llvm.commandline entry and make sure that this
1664 // requirement is met.
1665 for (const MDNode *N : CommandLines->operands()) {
1666 Check(N->getNumOperands() == 1,
1667 "incorrect number of operands in llvm.commandline metadata", N);
1668 Check(dyn_cast_or_null<MDString>(N->getOperand(0)),
1669 ("invalid value for llvm.commandline metadata entry operand"
1670 "(the operand should be a string)"),
1671 N->getOperand(0));
1672 }
1673 }
1674
visitModuleFlags()1675 void Verifier::visitModuleFlags() {
1676 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
1677 if (!Flags) return;
1678
1679 // Scan each flag, and track the flags and requirements.
1680 DenseMap<const MDString*, const MDNode*> SeenIDs;
1681 SmallVector<const MDNode*, 16> Requirements;
1682 for (const MDNode *MDN : Flags->operands())
1683 visitModuleFlag(MDN, SeenIDs, Requirements);
1684
1685 // Validate that the requirements in the module are valid.
1686 for (const MDNode *Requirement : Requirements) {
1687 const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1688 const Metadata *ReqValue = Requirement->getOperand(1);
1689
1690 const MDNode *Op = SeenIDs.lookup(Flag);
1691 if (!Op) {
1692 CheckFailed("invalid requirement on flag, flag is not present in module",
1693 Flag);
1694 continue;
1695 }
1696
1697 if (Op->getOperand(2) != ReqValue) {
1698 CheckFailed(("invalid requirement on flag, "
1699 "flag does not have the required value"),
1700 Flag);
1701 continue;
1702 }
1703 }
1704 }
1705
1706 void
visitModuleFlag(const MDNode * Op,DenseMap<const MDString *,const MDNode * > & SeenIDs,SmallVectorImpl<const MDNode * > & Requirements)1707 Verifier::visitModuleFlag(const MDNode *Op,
1708 DenseMap<const MDString *, const MDNode *> &SeenIDs,
1709 SmallVectorImpl<const MDNode *> &Requirements) {
1710 // Each module flag should have three arguments, the merge behavior (a
1711 // constant int), the flag ID (an MDString), and the value.
1712 Check(Op->getNumOperands() == 3,
1713 "incorrect number of operands in module flag", Op);
1714 Module::ModFlagBehavior MFB;
1715 if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
1716 Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
1717 "invalid behavior operand in module flag (expected constant integer)",
1718 Op->getOperand(0));
1719 Check(false,
1720 "invalid behavior operand in module flag (unexpected constant)",
1721 Op->getOperand(0));
1722 }
1723 MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1724 Check(ID, "invalid ID operand in module flag (expected metadata string)",
1725 Op->getOperand(1));
1726
1727 // Check the values for behaviors with additional requirements.
1728 switch (MFB) {
1729 case Module::Error:
1730 case Module::Warning:
1731 case Module::Override:
1732 // These behavior types accept any value.
1733 break;
1734
1735 case Module::Min: {
1736 auto *V = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1737 Check(V && V->getValue().isNonNegative(),
1738 "invalid value for 'min' module flag (expected constant non-negative "
1739 "integer)",
1740 Op->getOperand(2));
1741 break;
1742 }
1743
1744 case Module::Max: {
1745 Check(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
1746 "invalid value for 'max' module flag (expected constant integer)",
1747 Op->getOperand(2));
1748 break;
1749 }
1750
1751 case Module::Require: {
1752 // The value should itself be an MDNode with two operands, a flag ID (an
1753 // MDString), and a value.
1754 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
1755 Check(Value && Value->getNumOperands() == 2,
1756 "invalid value for 'require' module flag (expected metadata pair)",
1757 Op->getOperand(2));
1758 Check(isa<MDString>(Value->getOperand(0)),
1759 ("invalid value for 'require' module flag "
1760 "(first value operand should be a string)"),
1761 Value->getOperand(0));
1762
1763 // Append it to the list of requirements, to check once all module flags are
1764 // scanned.
1765 Requirements.push_back(Value);
1766 break;
1767 }
1768
1769 case Module::Append:
1770 case Module::AppendUnique: {
1771 // These behavior types require the operand be an MDNode.
1772 Check(isa<MDNode>(Op->getOperand(2)),
1773 "invalid value for 'append'-type module flag "
1774 "(expected a metadata node)",
1775 Op->getOperand(2));
1776 break;
1777 }
1778 }
1779
1780 // Unless this is a "requires" flag, check the ID is unique.
1781 if (MFB != Module::Require) {
1782 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
1783 Check(Inserted,
1784 "module flag identifiers must be unique (or of 'require' type)", ID);
1785 }
1786
1787 if (ID->getString() == "wchar_size") {
1788 ConstantInt *Value
1789 = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1790 Check(Value, "wchar_size metadata requires constant integer argument");
1791 }
1792
1793 if (ID->getString() == "Linker Options") {
1794 // If the llvm.linker.options named metadata exists, we assume that the
1795 // bitcode reader has upgraded the module flag. Otherwise the flag might
1796 // have been created by a client directly.
1797 Check(M.getNamedMetadata("llvm.linker.options"),
1798 "'Linker Options' named metadata no longer supported");
1799 }
1800
1801 if (ID->getString() == "SemanticInterposition") {
1802 ConstantInt *Value =
1803 mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1804 Check(Value,
1805 "SemanticInterposition metadata requires constant integer argument");
1806 }
1807
1808 if (ID->getString() == "CG Profile") {
1809 for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
1810 visitModuleFlagCGProfileEntry(MDO);
1811 }
1812 }
1813
visitModuleFlagCGProfileEntry(const MDOperand & MDO)1814 void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
1815 auto CheckFunction = [&](const MDOperand &FuncMDO) {
1816 if (!FuncMDO)
1817 return;
1818 auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
1819 Check(F && isa<Function>(F->getValue()->stripPointerCasts()),
1820 "expected a Function or null", FuncMDO);
1821 };
1822 auto Node = dyn_cast_or_null<MDNode>(MDO);
1823 Check(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
1824 CheckFunction(Node->getOperand(0));
1825 CheckFunction(Node->getOperand(1));
1826 auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
1827 Check(Count && Count->getType()->isIntegerTy(),
1828 "expected an integer constant", Node->getOperand(2));
1829 }
1830
verifyAttributeTypes(AttributeSet Attrs,const Value * V)1831 void Verifier::verifyAttributeTypes(AttributeSet Attrs, const Value *V) {
1832 for (Attribute A : Attrs) {
1833
1834 if (A.isStringAttribute()) {
1835 #define GET_ATTR_NAMES
1836 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)
1837 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \
1838 if (A.getKindAsString() == #DISPLAY_NAME) { \
1839 auto V = A.getValueAsString(); \
1840 if (!(V.empty() || V == "true" || V == "false")) \
1841 CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V + \
1842 ""); \
1843 }
1844
1845 #include "llvm/IR/Attributes.inc"
1846 continue;
1847 }
1848
1849 if (A.isIntAttribute() != Attribute::isIntAttrKind(A.getKindAsEnum())) {
1850 CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument",
1851 V);
1852 return;
1853 }
1854 }
1855 }
1856
1857 // VerifyParameterAttrs - Check the given attributes for an argument or return
1858 // value of the specified type. The value V is printed in error messages.
verifyParameterAttrs(AttributeSet Attrs,Type * Ty,const Value * V)1859 void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
1860 const Value *V) {
1861 if (!Attrs.hasAttributes())
1862 return;
1863
1864 verifyAttributeTypes(Attrs, V);
1865
1866 for (Attribute Attr : Attrs)
1867 Check(Attr.isStringAttribute() ||
1868 Attribute::canUseAsParamAttr(Attr.getKindAsEnum()),
1869 "Attribute '" + Attr.getAsString() + "' does not apply to parameters",
1870 V);
1871
1872 if (Attrs.hasAttribute(Attribute::ImmArg)) {
1873 Check(Attrs.getNumAttributes() == 1,
1874 "Attribute 'immarg' is incompatible with other attributes", V);
1875 }
1876
1877 // Check for mutually incompatible attributes. Only inreg is compatible with
1878 // sret.
1879 unsigned AttrCount = 0;
1880 AttrCount += Attrs.hasAttribute(Attribute::ByVal);
1881 AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
1882 AttrCount += Attrs.hasAttribute(Attribute::Preallocated);
1883 AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
1884 Attrs.hasAttribute(Attribute::InReg);
1885 AttrCount += Attrs.hasAttribute(Attribute::Nest);
1886 AttrCount += Attrs.hasAttribute(Attribute::ByRef);
1887 Check(AttrCount <= 1,
1888 "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', "
1889 "'byref', and 'sret' are incompatible!",
1890 V);
1891
1892 Check(!(Attrs.hasAttribute(Attribute::InAlloca) &&
1893 Attrs.hasAttribute(Attribute::ReadOnly)),
1894 "Attributes "
1895 "'inalloca and readonly' are incompatible!",
1896 V);
1897
1898 Check(!(Attrs.hasAttribute(Attribute::StructRet) &&
1899 Attrs.hasAttribute(Attribute::Returned)),
1900 "Attributes "
1901 "'sret and returned' are incompatible!",
1902 V);
1903
1904 Check(!(Attrs.hasAttribute(Attribute::ZExt) &&
1905 Attrs.hasAttribute(Attribute::SExt)),
1906 "Attributes "
1907 "'zeroext and signext' are incompatible!",
1908 V);
1909
1910 Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1911 Attrs.hasAttribute(Attribute::ReadOnly)),
1912 "Attributes "
1913 "'readnone and readonly' are incompatible!",
1914 V);
1915
1916 Check(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1917 Attrs.hasAttribute(Attribute::WriteOnly)),
1918 "Attributes "
1919 "'readnone and writeonly' are incompatible!",
1920 V);
1921
1922 Check(!(Attrs.hasAttribute(Attribute::ReadOnly) &&
1923 Attrs.hasAttribute(Attribute::WriteOnly)),
1924 "Attributes "
1925 "'readonly and writeonly' are incompatible!",
1926 V);
1927
1928 Check(!(Attrs.hasAttribute(Attribute::NoInline) &&
1929 Attrs.hasAttribute(Attribute::AlwaysInline)),
1930 "Attributes "
1931 "'noinline and alwaysinline' are incompatible!",
1932 V);
1933
1934 Check(!(Attrs.hasAttribute(Attribute::Writable) &&
1935 Attrs.hasAttribute(Attribute::ReadNone)),
1936 "Attributes writable and readnone are incompatible!", V);
1937
1938 Check(!(Attrs.hasAttribute(Attribute::Writable) &&
1939 Attrs.hasAttribute(Attribute::ReadOnly)),
1940 "Attributes writable and readonly are incompatible!", V);
1941
1942 AttributeMask IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
1943 for (Attribute Attr : Attrs) {
1944 if (!Attr.isStringAttribute() &&
1945 IncompatibleAttrs.contains(Attr.getKindAsEnum())) {
1946 CheckFailed("Attribute '" + Attr.getAsString() +
1947 "' applied to incompatible type!", V);
1948 return;
1949 }
1950 }
1951
1952 if (isa<PointerType>(Ty)) {
1953 if (Attrs.hasAttribute(Attribute::ByVal)) {
1954 if (Attrs.hasAttribute(Attribute::Alignment)) {
1955 Align AttrAlign = Attrs.getAlignment().valueOrOne();
1956 Align MaxAlign(ParamMaxAlignment);
1957 Check(AttrAlign <= MaxAlign,
1958 "Attribute 'align' exceed the max size 2^14", V);
1959 }
1960 SmallPtrSet<Type *, 4> Visited;
1961 Check(Attrs.getByValType()->isSized(&Visited),
1962 "Attribute 'byval' does not support unsized types!", V);
1963 }
1964 if (Attrs.hasAttribute(Attribute::ByRef)) {
1965 SmallPtrSet<Type *, 4> Visited;
1966 Check(Attrs.getByRefType()->isSized(&Visited),
1967 "Attribute 'byref' does not support unsized types!", V);
1968 }
1969 if (Attrs.hasAttribute(Attribute::InAlloca)) {
1970 SmallPtrSet<Type *, 4> Visited;
1971 Check(Attrs.getInAllocaType()->isSized(&Visited),
1972 "Attribute 'inalloca' does not support unsized types!", V);
1973 }
1974 if (Attrs.hasAttribute(Attribute::Preallocated)) {
1975 SmallPtrSet<Type *, 4> Visited;
1976 Check(Attrs.getPreallocatedType()->isSized(&Visited),
1977 "Attribute 'preallocated' does not support unsized types!", V);
1978 }
1979 }
1980
1981 if (Attrs.hasAttribute(Attribute::NoFPClass)) {
1982 uint64_t Val = Attrs.getAttribute(Attribute::NoFPClass).getValueAsInt();
1983 Check(Val != 0, "Attribute 'nofpclass' must have at least one test bit set",
1984 V);
1985 Check((Val & ~static_cast<unsigned>(fcAllFlags)) == 0,
1986 "Invalid value for 'nofpclass' test mask", V);
1987 }
1988 }
1989
checkUnsignedBaseTenFuncAttr(AttributeList Attrs,StringRef Attr,const Value * V)1990 void Verifier::checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
1991 const Value *V) {
1992 if (Attrs.hasFnAttr(Attr)) {
1993 StringRef S = Attrs.getFnAttr(Attr).getValueAsString();
1994 unsigned N;
1995 if (S.getAsInteger(10, N))
1996 CheckFailed("\"" + Attr + "\" takes an unsigned integer: " + S, V);
1997 }
1998 }
1999
2000 // Check parameter attributes against a function type.
2001 // The value V is printed in error messages.
verifyFunctionAttrs(FunctionType * FT,AttributeList Attrs,const Value * V,bool IsIntrinsic,bool IsInlineAsm)2002 void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
2003 const Value *V, bool IsIntrinsic,
2004 bool IsInlineAsm) {
2005 if (Attrs.isEmpty())
2006 return;
2007
2008 if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
2009 Check(Attrs.hasParentContext(Context),
2010 "Attribute list does not match Module context!", &Attrs, V);
2011 for (const auto &AttrSet : Attrs) {
2012 Check(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
2013 "Attribute set does not match Module context!", &AttrSet, V);
2014 for (const auto &A : AttrSet) {
2015 Check(A.hasParentContext(Context),
2016 "Attribute does not match Module context!", &A, V);
2017 }
2018 }
2019 }
2020
2021 bool SawNest = false;
2022 bool SawReturned = false;
2023 bool SawSRet = false;
2024 bool SawSwiftSelf = false;
2025 bool SawSwiftAsync = false;
2026 bool SawSwiftError = false;
2027
2028 // Verify return value attributes.
2029 AttributeSet RetAttrs = Attrs.getRetAttrs();
2030 for (Attribute RetAttr : RetAttrs)
2031 Check(RetAttr.isStringAttribute() ||
2032 Attribute::canUseAsRetAttr(RetAttr.getKindAsEnum()),
2033 "Attribute '" + RetAttr.getAsString() +
2034 "' does not apply to function return values",
2035 V);
2036
2037 unsigned MaxParameterWidth = 0;
2038 auto GetMaxParameterWidth = [&MaxParameterWidth](Type *Ty) {
2039 if (Ty->isVectorTy()) {
2040 if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
2041 unsigned Size = VT->getPrimitiveSizeInBits().getFixedValue();
2042 if (Size > MaxParameterWidth)
2043 MaxParameterWidth = Size;
2044 }
2045 }
2046 };
2047 GetMaxParameterWidth(FT->getReturnType());
2048 verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
2049
2050 // Verify parameter attributes.
2051 for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
2052 Type *Ty = FT->getParamType(i);
2053 AttributeSet ArgAttrs = Attrs.getParamAttrs(i);
2054
2055 if (!IsIntrinsic) {
2056 Check(!ArgAttrs.hasAttribute(Attribute::ImmArg),
2057 "immarg attribute only applies to intrinsics", V);
2058 if (!IsInlineAsm)
2059 Check(!ArgAttrs.hasAttribute(Attribute::ElementType),
2060 "Attribute 'elementtype' can only be applied to intrinsics"
2061 " and inline asm.",
2062 V);
2063 }
2064
2065 verifyParameterAttrs(ArgAttrs, Ty, V);
2066 GetMaxParameterWidth(Ty);
2067
2068 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
2069 Check(!SawNest, "More than one parameter has attribute nest!", V);
2070 SawNest = true;
2071 }
2072
2073 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
2074 Check(!SawReturned, "More than one parameter has attribute returned!", V);
2075 Check(Ty->canLosslesslyBitCastTo(FT->getReturnType()),
2076 "Incompatible argument and return types for 'returned' attribute",
2077 V);
2078 SawReturned = true;
2079 }
2080
2081 if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
2082 Check(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
2083 Check(i == 0 || i == 1,
2084 "Attribute 'sret' is not on first or second parameter!", V);
2085 SawSRet = true;
2086 }
2087
2088 if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
2089 Check(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
2090 SawSwiftSelf = true;
2091 }
2092
2093 if (ArgAttrs.hasAttribute(Attribute::SwiftAsync)) {
2094 Check(!SawSwiftAsync, "Cannot have multiple 'swiftasync' parameters!", V);
2095 SawSwiftAsync = true;
2096 }
2097
2098 if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
2099 Check(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!", V);
2100 SawSwiftError = true;
2101 }
2102
2103 if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
2104 Check(i == FT->getNumParams() - 1,
2105 "inalloca isn't on the last parameter!", V);
2106 }
2107 }
2108
2109 if (!Attrs.hasFnAttrs())
2110 return;
2111
2112 verifyAttributeTypes(Attrs.getFnAttrs(), V);
2113 for (Attribute FnAttr : Attrs.getFnAttrs())
2114 Check(FnAttr.isStringAttribute() ||
2115 Attribute::canUseAsFnAttr(FnAttr.getKindAsEnum()),
2116 "Attribute '" + FnAttr.getAsString() +
2117 "' does not apply to functions!",
2118 V);
2119
2120 Check(!(Attrs.hasFnAttr(Attribute::NoInline) &&
2121 Attrs.hasFnAttr(Attribute::AlwaysInline)),
2122 "Attributes 'noinline and alwaysinline' are incompatible!", V);
2123
2124 if (Attrs.hasFnAttr(Attribute::OptimizeNone)) {
2125 Check(Attrs.hasFnAttr(Attribute::NoInline),
2126 "Attribute 'optnone' requires 'noinline'!", V);
2127
2128 Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
2129 "Attributes 'optsize and optnone' are incompatible!", V);
2130
2131 Check(!Attrs.hasFnAttr(Attribute::MinSize),
2132 "Attributes 'minsize and optnone' are incompatible!", V);
2133
2134 Check(!Attrs.hasFnAttr(Attribute::OptimizeForDebugging),
2135 "Attributes 'optdebug and optnone' are incompatible!", V);
2136 }
2137
2138 if (Attrs.hasFnAttr(Attribute::OptimizeForDebugging)) {
2139 Check(!Attrs.hasFnAttr(Attribute::OptimizeForSize),
2140 "Attributes 'optsize and optdebug' are incompatible!", V);
2141
2142 Check(!Attrs.hasFnAttr(Attribute::MinSize),
2143 "Attributes 'minsize and optdebug' are incompatible!", V);
2144 }
2145
2146 Check(!Attrs.hasAttrSomewhere(Attribute::Writable) ||
2147 isModSet(Attrs.getMemoryEffects().getModRef(IRMemLocation::ArgMem)),
2148 "Attribute writable and memory without argmem: write are incompatible!",
2149 V);
2150
2151 if (Attrs.hasFnAttr("aarch64_pstate_sm_enabled")) {
2152 Check(!Attrs.hasFnAttr("aarch64_pstate_sm_compatible"),
2153 "Attributes 'aarch64_pstate_sm_enabled and "
2154 "aarch64_pstate_sm_compatible' are incompatible!",
2155 V);
2156 }
2157
2158 if (Attrs.hasFnAttr("aarch64_pstate_za_new")) {
2159 Check(!Attrs.hasFnAttr("aarch64_pstate_za_preserved"),
2160 "Attributes 'aarch64_pstate_za_new and aarch64_pstate_za_preserved' "
2161 "are incompatible!",
2162 V);
2163
2164 Check(!Attrs.hasFnAttr("aarch64_pstate_za_shared"),
2165 "Attributes 'aarch64_pstate_za_new and aarch64_pstate_za_shared' "
2166 "are incompatible!",
2167 V);
2168 }
2169
2170 Check(
2171 (Attrs.hasFnAttr("aarch64_new_zt0") + Attrs.hasFnAttr("aarch64_in_zt0") +
2172 Attrs.hasFnAttr("aarch64_inout_zt0") +
2173 Attrs.hasFnAttr("aarch64_out_zt0") +
2174 Attrs.hasFnAttr("aarch64_preserves_zt0")) <= 1,
2175 "Attributes 'aarch64_new_zt0', 'aarch64_in_zt0', 'aarch64_out_zt0', "
2176 "'aarch64_inout_zt0' and 'aarch64_preserves_zt0' are mutually exclusive",
2177 V);
2178
2179 if (Attrs.hasFnAttr(Attribute::JumpTable)) {
2180 const GlobalValue *GV = cast<GlobalValue>(V);
2181 Check(GV->hasGlobalUnnamedAddr(),
2182 "Attribute 'jumptable' requires 'unnamed_addr'", V);
2183 }
2184
2185 if (auto Args = Attrs.getFnAttrs().getAllocSizeArgs()) {
2186 auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
2187 if (ParamNo >= FT->getNumParams()) {
2188 CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
2189 return false;
2190 }
2191
2192 if (!FT->getParamType(ParamNo)->isIntegerTy()) {
2193 CheckFailed("'allocsize' " + Name +
2194 " argument must refer to an integer parameter",
2195 V);
2196 return false;
2197 }
2198
2199 return true;
2200 };
2201
2202 if (!CheckParam("element size", Args->first))
2203 return;
2204
2205 if (Args->second && !CheckParam("number of elements", *Args->second))
2206 return;
2207 }
2208
2209 if (Attrs.hasFnAttr(Attribute::AllocKind)) {
2210 AllocFnKind K = Attrs.getAllocKind();
2211 AllocFnKind Type =
2212 K & (AllocFnKind::Alloc | AllocFnKind::Realloc | AllocFnKind::Free);
2213 if (!is_contained(
2214 {AllocFnKind::Alloc, AllocFnKind::Realloc, AllocFnKind::Free},
2215 Type))
2216 CheckFailed(
2217 "'allockind()' requires exactly one of alloc, realloc, and free");
2218 if ((Type == AllocFnKind::Free) &&
2219 ((K & (AllocFnKind::Uninitialized | AllocFnKind::Zeroed |
2220 AllocFnKind::Aligned)) != AllocFnKind::Unknown))
2221 CheckFailed("'allockind(\"free\")' doesn't allow uninitialized, zeroed, "
2222 "or aligned modifiers.");
2223 AllocFnKind ZeroedUninit = AllocFnKind::Uninitialized | AllocFnKind::Zeroed;
2224 if ((K & ZeroedUninit) == ZeroedUninit)
2225 CheckFailed("'allockind()' can't be both zeroed and uninitialized");
2226 }
2227
2228 if (Attrs.hasFnAttr(Attribute::VScaleRange)) {
2229 unsigned VScaleMin = Attrs.getFnAttrs().getVScaleRangeMin();
2230 if (VScaleMin == 0)
2231 CheckFailed("'vscale_range' minimum must be greater than 0", V);
2232 else if (!isPowerOf2_32(VScaleMin))
2233 CheckFailed("'vscale_range' minimum must be power-of-two value", V);
2234 std::optional<unsigned> VScaleMax = Attrs.getFnAttrs().getVScaleRangeMax();
2235 if (VScaleMax && VScaleMin > VScaleMax)
2236 CheckFailed("'vscale_range' minimum cannot be greater than maximum", V);
2237 else if (VScaleMax && !isPowerOf2_32(*VScaleMax))
2238 CheckFailed("'vscale_range' maximum must be power-of-two value", V);
2239 }
2240
2241 if (Attrs.hasFnAttr("frame-pointer")) {
2242 StringRef FP = Attrs.getFnAttr("frame-pointer").getValueAsString();
2243 if (FP != "all" && FP != "non-leaf" && FP != "none")
2244 CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V);
2245 }
2246
2247 // Check EVEX512 feature.
2248 if (MaxParameterWidth >= 512 && Attrs.hasFnAttr("target-features") &&
2249 TT.isX86()) {
2250 StringRef TF = Attrs.getFnAttr("target-features").getValueAsString();
2251 Check(!TF.contains("+avx512f") || !TF.contains("-evex512"),
2252 "512-bit vector arguments require 'evex512' for AVX512", V);
2253 }
2254
2255 checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V);
2256 checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
2257 checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);
2258
2259 if (auto A = Attrs.getFnAttr("sign-return-address"); A.isValid()) {
2260 StringRef S = A.getValueAsString();
2261 if (S != "none" && S != "all" && S != "non-leaf")
2262 CheckFailed("invalid value for 'sign-return-address' attribute: " + S, V);
2263 }
2264
2265 if (auto A = Attrs.getFnAttr("sign-return-address-key"); A.isValid()) {
2266 StringRef S = A.getValueAsString();
2267 if (S != "a_key" && S != "b_key")
2268 CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
2269 V);
2270 }
2271
2272 if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
2273 StringRef S = A.getValueAsString();
2274 if (S != "true" && S != "false")
2275 CheckFailed(
2276 "invalid value for 'branch-target-enforcement' attribute: " + S, V);
2277 }
2278
2279 if (auto A = Attrs.getFnAttr("vector-function-abi-variant"); A.isValid()) {
2280 StringRef S = A.getValueAsString();
2281 const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(S, FT);
2282 if (!Info)
2283 CheckFailed("invalid name for a VFABI variant: " + S, V);
2284 }
2285 }
2286
verifyFunctionMetadata(ArrayRef<std::pair<unsigned,MDNode * >> MDs)2287 void Verifier::verifyFunctionMetadata(
2288 ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
2289 for (const auto &Pair : MDs) {
2290 if (Pair.first == LLVMContext::MD_prof) {
2291 MDNode *MD = Pair.second;
2292 Check(MD->getNumOperands() >= 2,
2293 "!prof annotations should have no less than 2 operands", MD);
2294
2295 // Check first operand.
2296 Check(MD->getOperand(0) != nullptr, "first operand should not be null",
2297 MD);
2298 Check(isa<MDString>(MD->getOperand(0)),
2299 "expected string with name of the !prof annotation", MD);
2300 MDString *MDS = cast<MDString>(MD->getOperand(0));
2301 StringRef ProfName = MDS->getString();
2302 Check(ProfName.equals("function_entry_count") ||
2303 ProfName.equals("synthetic_function_entry_count"),
2304 "first operand should be 'function_entry_count'"
2305 " or 'synthetic_function_entry_count'",
2306 MD);
2307
2308 // Check second operand.
2309 Check(MD->getOperand(1) != nullptr, "second operand should not be null",
2310 MD);
2311 Check(isa<ConstantAsMetadata>(MD->getOperand(1)),
2312 "expected integer argument to function_entry_count", MD);
2313 } else if (Pair.first == LLVMContext::MD_kcfi_type) {
2314 MDNode *MD = Pair.second;
2315 Check(MD->getNumOperands() == 1,
2316 "!kcfi_type must have exactly one operand", MD);
2317 Check(MD->getOperand(0) != nullptr, "!kcfi_type operand must not be null",
2318 MD);
2319 Check(isa<ConstantAsMetadata>(MD->getOperand(0)),
2320 "expected a constant operand for !kcfi_type", MD);
2321 Constant *C = cast<ConstantAsMetadata>(MD->getOperand(0))->getValue();
2322 Check(isa<ConstantInt>(C) && isa<IntegerType>(C->getType()),
2323 "expected a constant integer operand for !kcfi_type", MD);
2324 Check(cast<ConstantInt>(C)->getBitWidth() == 32,
2325 "expected a 32-bit integer constant operand for !kcfi_type", MD);
2326 }
2327 }
2328 }
2329
visitConstantExprsRecursively(const Constant * EntryC)2330 void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
2331 if (!ConstantExprVisited.insert(EntryC).second)
2332 return;
2333
2334 SmallVector<const Constant *, 16> Stack;
2335 Stack.push_back(EntryC);
2336
2337 while (!Stack.empty()) {
2338 const Constant *C = Stack.pop_back_val();
2339
2340 // Check this constant expression.
2341 if (const auto *CE = dyn_cast<ConstantExpr>(C))
2342 visitConstantExpr(CE);
2343
2344 if (const auto *GV = dyn_cast<GlobalValue>(C)) {
2345 // Global Values get visited separately, but we do need to make sure
2346 // that the global value is in the correct module
2347 Check(GV->getParent() == &M, "Referencing global in another module!",
2348 EntryC, &M, GV, GV->getParent());
2349 continue;
2350 }
2351
2352 // Visit all sub-expressions.
2353 for (const Use &U : C->operands()) {
2354 const auto *OpC = dyn_cast<Constant>(U);
2355 if (!OpC)
2356 continue;
2357 if (!ConstantExprVisited.insert(OpC).second)
2358 continue;
2359 Stack.push_back(OpC);
2360 }
2361 }
2362 }
2363
visitConstantExpr(const ConstantExpr * CE)2364 void Verifier::visitConstantExpr(const ConstantExpr *CE) {
2365 if (CE->getOpcode() == Instruction::BitCast)
2366 Check(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
2367 CE->getType()),
2368 "Invalid bitcast", CE);
2369 }
2370
verifyAttributeCount(AttributeList Attrs,unsigned Params)2371 bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
2372 // There shouldn't be more attribute sets than there are parameters plus the
2373 // function and return value.
2374 return Attrs.getNumAttrSets() <= Params + 2;
2375 }
2376
verifyInlineAsmCall(const CallBase & Call)2377 void Verifier::verifyInlineAsmCall(const CallBase &Call) {
2378 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
2379 unsigned ArgNo = 0;
2380 unsigned LabelNo = 0;
2381 for (const InlineAsm::ConstraintInfo &CI : IA->ParseConstraints()) {
2382 if (CI.Type == InlineAsm::isLabel) {
2383 ++LabelNo;
2384 continue;
2385 }
2386
2387 // Only deal with constraints that correspond to call arguments.
2388 if (!CI.hasArg())
2389 continue;
2390
2391 if (CI.isIndirect) {
2392 const Value *Arg = Call.getArgOperand(ArgNo);
2393 Check(Arg->getType()->isPointerTy(),
2394 "Operand for indirect constraint must have pointer type", &Call);
2395
2396 Check(Call.getParamElementType(ArgNo),
2397 "Operand for indirect constraint must have elementtype attribute",
2398 &Call);
2399 } else {
2400 Check(!Call.paramHasAttr(ArgNo, Attribute::ElementType),
2401 "Elementtype attribute can only be applied for indirect "
2402 "constraints",
2403 &Call);
2404 }
2405
2406 ArgNo++;
2407 }
2408
2409 if (auto *CallBr = dyn_cast<CallBrInst>(&Call)) {
2410 Check(LabelNo == CallBr->getNumIndirectDests(),
2411 "Number of label constraints does not match number of callbr dests",
2412 &Call);
2413 } else {
2414 Check(LabelNo == 0, "Label constraints can only be used with callbr",
2415 &Call);
2416 }
2417 }
2418
2419 /// Verify that statepoint intrinsic is well formed.
verifyStatepoint(const CallBase & Call)2420 void Verifier::verifyStatepoint(const CallBase &Call) {
2421 assert(Call.getCalledFunction() &&
2422 Call.getCalledFunction()->getIntrinsicID() ==
2423 Intrinsic::experimental_gc_statepoint);
2424
2425 Check(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
2426 !Call.onlyAccessesArgMemory(),
2427 "gc.statepoint must read and write all memory to preserve "
2428 "reordering restrictions required by safepoint semantics",
2429 Call);
2430
2431 const int64_t NumPatchBytes =
2432 cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
2433 assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
2434 Check(NumPatchBytes >= 0,
2435 "gc.statepoint number of patchable bytes must be "
2436 "positive",
2437 Call);
2438
2439 Type *TargetElemType = Call.getParamElementType(2);
2440 Check(TargetElemType,
2441 "gc.statepoint callee argument must have elementtype attribute", Call);
2442 FunctionType *TargetFuncType = dyn_cast<FunctionType>(TargetElemType);
2443 Check(TargetFuncType,
2444 "gc.statepoint callee elementtype must be function type", Call);
2445
2446 const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
2447 Check(NumCallArgs >= 0,
2448 "gc.statepoint number of arguments to underlying call "
2449 "must be positive",
2450 Call);
2451 const int NumParams = (int)TargetFuncType->getNumParams();
2452 if (TargetFuncType->isVarArg()) {
2453 Check(NumCallArgs >= NumParams,
2454 "gc.statepoint mismatch in number of vararg call args", Call);
2455
2456 // TODO: Remove this limitation
2457 Check(TargetFuncType->getReturnType()->isVoidTy(),
2458 "gc.statepoint doesn't support wrapping non-void "
2459 "vararg functions yet",
2460 Call);
2461 } else
2462 Check(NumCallArgs == NumParams,
2463 "gc.statepoint mismatch in number of call args", Call);
2464
2465 const uint64_t Flags
2466 = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
2467 Check((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
2468 "unknown flag used in gc.statepoint flags argument", Call);
2469
2470 // Verify that the types of the call parameter arguments match
2471 // the type of the wrapped callee.
2472 AttributeList Attrs = Call.getAttributes();
2473 for (int i = 0; i < NumParams; i++) {
2474 Type *ParamType = TargetFuncType->getParamType(i);
2475 Type *ArgType = Call.getArgOperand(5 + i)->getType();
2476 Check(ArgType == ParamType,
2477 "gc.statepoint call argument does not match wrapped "
2478 "function type",
2479 Call);
2480
2481 if (TargetFuncType->isVarArg()) {
2482 AttributeSet ArgAttrs = Attrs.getParamAttrs(5 + i);
2483 Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
2484 "Attribute 'sret' cannot be used for vararg call arguments!", Call);
2485 }
2486 }
2487
2488 const int EndCallArgsInx = 4 + NumCallArgs;
2489
2490 const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
2491 Check(isa<ConstantInt>(NumTransitionArgsV),
2492 "gc.statepoint number of transition arguments "
2493 "must be constant integer",
2494 Call);
2495 const int NumTransitionArgs =
2496 cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
2497 Check(NumTransitionArgs == 0,
2498 "gc.statepoint w/inline transition bundle is deprecated", Call);
2499 const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
2500
2501 const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
2502 Check(isa<ConstantInt>(NumDeoptArgsV),
2503 "gc.statepoint number of deoptimization arguments "
2504 "must be constant integer",
2505 Call);
2506 const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
2507 Check(NumDeoptArgs == 0,
2508 "gc.statepoint w/inline deopt operands is deprecated", Call);
2509
2510 const int ExpectedNumArgs = 7 + NumCallArgs;
2511 Check(ExpectedNumArgs == (int)Call.arg_size(),
2512 "gc.statepoint too many arguments", Call);
2513
2514 // Check that the only uses of this gc.statepoint are gc.result or
2515 // gc.relocate calls which are tied to this statepoint and thus part
2516 // of the same statepoint sequence
2517 for (const User *U : Call.users()) {
2518 const CallInst *UserCall = dyn_cast<const CallInst>(U);
2519 Check(UserCall, "illegal use of statepoint token", Call, U);
2520 if (!UserCall)
2521 continue;
2522 Check(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
2523 "gc.result or gc.relocate are the only value uses "
2524 "of a gc.statepoint",
2525 Call, U);
2526 if (isa<GCResultInst>(UserCall)) {
2527 Check(UserCall->getArgOperand(0) == &Call,
2528 "gc.result connected to wrong gc.statepoint", Call, UserCall);
2529 } else if (isa<GCRelocateInst>(Call)) {
2530 Check(UserCall->getArgOperand(0) == &Call,
2531 "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
2532 }
2533 }
2534
2535 // Note: It is legal for a single derived pointer to be listed multiple
2536 // times. It's non-optimal, but it is legal. It can also happen after
2537 // insertion if we strip a bitcast away.
2538 // Note: It is really tempting to check that each base is relocated and
2539 // that a derived pointer is never reused as a base pointer. This turns
2540 // out to be problematic since optimizations run after safepoint insertion
2541 // can recognize equality properties that the insertion logic doesn't know
2542 // about. See example statepoint.ll in the verifier subdirectory
2543 }
2544
verifyFrameRecoverIndices()2545 void Verifier::verifyFrameRecoverIndices() {
2546 for (auto &Counts : FrameEscapeInfo) {
2547 Function *F = Counts.first;
2548 unsigned EscapedObjectCount = Counts.second.first;
2549 unsigned MaxRecoveredIndex = Counts.second.second;
2550 Check(MaxRecoveredIndex <= EscapedObjectCount,
2551 "all indices passed to llvm.localrecover must be less than the "
2552 "number of arguments passed to llvm.localescape in the parent "
2553 "function",
2554 F);
2555 }
2556 }
2557
getSuccPad(Instruction * Terminator)2558 static Instruction *getSuccPad(Instruction *Terminator) {
2559 BasicBlock *UnwindDest;
2560 if (auto *II = dyn_cast<InvokeInst>(Terminator))
2561 UnwindDest = II->getUnwindDest();
2562 else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
2563 UnwindDest = CSI->getUnwindDest();
2564 else
2565 UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
2566 return UnwindDest->getFirstNonPHI();
2567 }
2568
verifySiblingFuncletUnwinds()2569 void Verifier::verifySiblingFuncletUnwinds() {
2570 SmallPtrSet<Instruction *, 8> Visited;
2571 SmallPtrSet<Instruction *, 8> Active;
2572 for (const auto &Pair : SiblingFuncletInfo) {
2573 Instruction *PredPad = Pair.first;
2574 if (Visited.count(PredPad))
2575 continue;
2576 Active.insert(PredPad);
2577 Instruction *Terminator = Pair.second;
2578 do {
2579 Instruction *SuccPad = getSuccPad(Terminator);
2580 if (Active.count(SuccPad)) {
2581 // Found a cycle; report error
2582 Instruction *CyclePad = SuccPad;
2583 SmallVector<Instruction *, 8> CycleNodes;
2584 do {
2585 CycleNodes.push_back(CyclePad);
2586 Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
2587 if (CycleTerminator != CyclePad)
2588 CycleNodes.push_back(CycleTerminator);
2589 CyclePad = getSuccPad(CycleTerminator);
2590 } while (CyclePad != SuccPad);
2591 Check(false, "EH pads can't handle each other's exceptions",
2592 ArrayRef<Instruction *>(CycleNodes));
2593 }
2594 // Don't re-walk a node we've already checked
2595 if (!Visited.insert(SuccPad).second)
2596 break;
2597 // Walk to this successor if it has a map entry.
2598 PredPad = SuccPad;
2599 auto TermI = SiblingFuncletInfo.find(PredPad);
2600 if (TermI == SiblingFuncletInfo.end())
2601 break;
2602 Terminator = TermI->second;
2603 Active.insert(PredPad);
2604 } while (true);
2605 // Each node only has one successor, so we've walked all the active
2606 // nodes' successors.
2607 Active.clear();
2608 }
2609 }
2610
2611 // visitFunction - Verify that a function is ok.
2612 //
visitFunction(const Function & F)2613 void Verifier::visitFunction(const Function &F) {
2614 visitGlobalValue(F);
2615
2616 // Check function arguments.
2617 FunctionType *FT = F.getFunctionType();
2618 unsigned NumArgs = F.arg_size();
2619
2620 Check(&Context == &F.getContext(),
2621 "Function context does not match Module context!", &F);
2622
2623 Check(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
2624 Check(FT->getNumParams() == NumArgs,
2625 "# formal arguments must match # of arguments for function type!", &F,
2626 FT);
2627 Check(F.getReturnType()->isFirstClassType() ||
2628 F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
2629 "Functions cannot return aggregate values!", &F);
2630
2631 Check(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
2632 "Invalid struct return type!", &F);
2633
2634 AttributeList Attrs = F.getAttributes();
2635
2636 Check(verifyAttributeCount(Attrs, FT->getNumParams()),
2637 "Attribute after last parameter!", &F);
2638
2639 bool IsIntrinsic = F.isIntrinsic();
2640
2641 // Check function attributes.
2642 verifyFunctionAttrs(FT, Attrs, &F, IsIntrinsic, /* IsInlineAsm */ false);
2643
2644 // On function declarations/definitions, we do not support the builtin
2645 // attribute. We do not check this in VerifyFunctionAttrs since that is
2646 // checking for Attributes that can/can not ever be on functions.
2647 Check(!Attrs.hasFnAttr(Attribute::Builtin),
2648 "Attribute 'builtin' can only be applied to a callsite.", &F);
2649
2650 Check(!Attrs.hasAttrSomewhere(Attribute::ElementType),
2651 "Attribute 'elementtype' can only be applied to a callsite.", &F);
2652
2653 // Check that this function meets the restrictions on this calling convention.
2654 // Sometimes varargs is used for perfectly forwarding thunks, so some of these
2655 // restrictions can be lifted.
2656 switch (F.getCallingConv()) {
2657 default:
2658 case CallingConv::C:
2659 break;
2660 case CallingConv::X86_INTR: {
2661 Check(F.arg_empty() || Attrs.hasParamAttr(0, Attribute::ByVal),
2662 "Calling convention parameter requires byval", &F);
2663 break;
2664 }
2665 case CallingConv::AMDGPU_KERNEL:
2666 case CallingConv::SPIR_KERNEL:
2667 case CallingConv::AMDGPU_CS_Chain:
2668 case CallingConv::AMDGPU_CS_ChainPreserve:
2669 Check(F.getReturnType()->isVoidTy(),
2670 "Calling convention requires void return type", &F);
2671 [[fallthrough]];
2672 case CallingConv::AMDGPU_VS:
2673 case CallingConv::AMDGPU_HS:
2674 case CallingConv::AMDGPU_GS:
2675 case CallingConv::AMDGPU_PS:
2676 case CallingConv::AMDGPU_CS:
2677 Check(!F.hasStructRetAttr(), "Calling convention does not allow sret", &F);
2678 if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
2679 const unsigned StackAS = DL.getAllocaAddrSpace();
2680 unsigned i = 0;
2681 for (const Argument &Arg : F.args()) {
2682 Check(!Attrs.hasParamAttr(i, Attribute::ByVal),
2683 "Calling convention disallows byval", &F);
2684 Check(!Attrs.hasParamAttr(i, Attribute::Preallocated),
2685 "Calling convention disallows preallocated", &F);
2686 Check(!Attrs.hasParamAttr(i, Attribute::InAlloca),
2687 "Calling convention disallows inalloca", &F);
2688
2689 if (Attrs.hasParamAttr(i, Attribute::ByRef)) {
2690 // FIXME: Should also disallow LDS and GDS, but we don't have the enum
2691 // value here.
2692 Check(Arg.getType()->getPointerAddressSpace() != StackAS,
2693 "Calling convention disallows stack byref", &F);
2694 }
2695
2696 ++i;
2697 }
2698 }
2699
2700 [[fallthrough]];
2701 case CallingConv::Fast:
2702 case CallingConv::Cold:
2703 case CallingConv::Intel_OCL_BI:
2704 case CallingConv::PTX_Kernel:
2705 case CallingConv::PTX_Device:
2706 Check(!F.isVarArg(),
2707 "Calling convention does not support varargs or "
2708 "perfect forwarding!",
2709 &F);
2710 break;
2711 }
2712
2713 // Check that the argument values match the function type for this function...
2714 unsigned i = 0;
2715 for (const Argument &Arg : F.args()) {
2716 Check(Arg.getType() == FT->getParamType(i),
2717 "Argument value does not match function argument type!", &Arg,
2718 FT->getParamType(i));
2719 Check(Arg.getType()->isFirstClassType(),
2720 "Function arguments must have first-class types!", &Arg);
2721 if (!IsIntrinsic) {
2722 Check(!Arg.getType()->isMetadataTy(),
2723 "Function takes metadata but isn't an intrinsic", &Arg, &F);
2724 Check(!Arg.getType()->isTokenTy(),
2725 "Function takes token but isn't an intrinsic", &Arg, &F);
2726 Check(!Arg.getType()->isX86_AMXTy(),
2727 "Function takes x86_amx but isn't an intrinsic", &Arg, &F);
2728 }
2729
2730 // Check that swifterror argument is only used by loads and stores.
2731 if (Attrs.hasParamAttr(i, Attribute::SwiftError)) {
2732 verifySwiftErrorValue(&Arg);
2733 }
2734 ++i;
2735 }
2736
2737 if (!IsIntrinsic) {
2738 Check(!F.getReturnType()->isTokenTy(),
2739 "Function returns a token but isn't an intrinsic", &F);
2740 Check(!F.getReturnType()->isX86_AMXTy(),
2741 "Function returns a x86_amx but isn't an intrinsic", &F);
2742 }
2743
2744 // Get the function metadata attachments.
2745 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
2746 F.getAllMetadata(MDs);
2747 assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
2748 verifyFunctionMetadata(MDs);
2749
2750 // Check validity of the personality function
2751 if (F.hasPersonalityFn()) {
2752 auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
2753 if (Per)
2754 Check(Per->getParent() == F.getParent(),
2755 "Referencing personality function in another module!", &F,
2756 F.getParent(), Per, Per->getParent());
2757 }
2758
2759 // EH funclet coloring can be expensive, recompute on-demand
2760 BlockEHFuncletColors.clear();
2761
2762 if (F.isMaterializable()) {
2763 // Function has a body somewhere we can't see.
2764 Check(MDs.empty(), "unmaterialized function cannot have metadata", &F,
2765 MDs.empty() ? nullptr : MDs.front().second);
2766 } else if (F.isDeclaration()) {
2767 for (const auto &I : MDs) {
2768 // This is used for call site debug information.
2769 CheckDI(I.first != LLVMContext::MD_dbg ||
2770 !cast<DISubprogram>(I.second)->isDistinct(),
2771 "function declaration may only have a unique !dbg attachment",
2772 &F);
2773 Check(I.first != LLVMContext::MD_prof,
2774 "function declaration may not have a !prof attachment", &F);
2775
2776 // Verify the metadata itself.
2777 visitMDNode(*I.second, AreDebugLocsAllowed::Yes);
2778 }
2779 Check(!F.hasPersonalityFn(),
2780 "Function declaration shouldn't have a personality routine", &F);
2781 } else {
2782 // Verify that this function (which has a body) is not named "llvm.*". It
2783 // is not legal to define intrinsics.
2784 Check(!IsIntrinsic, "llvm intrinsics cannot be defined!", &F);
2785
2786 // Check the entry node
2787 const BasicBlock *Entry = &F.getEntryBlock();
2788 Check(pred_empty(Entry),
2789 "Entry block to function must not have predecessors!", Entry);
2790
2791 // The address of the entry block cannot be taken, unless it is dead.
2792 if (Entry->hasAddressTaken()) {
2793 Check(!BlockAddress::lookup(Entry)->isConstantUsed(),
2794 "blockaddress may not be used with the entry block!", Entry);
2795 }
2796
2797 unsigned NumDebugAttachments = 0, NumProfAttachments = 0,
2798 NumKCFIAttachments = 0;
2799 // Visit metadata attachments.
2800 for (const auto &I : MDs) {
2801 // Verify that the attachment is legal.
2802 auto AllowLocs = AreDebugLocsAllowed::No;
2803 switch (I.first) {
2804 default:
2805 break;
2806 case LLVMContext::MD_dbg: {
2807 ++NumDebugAttachments;
2808 CheckDI(NumDebugAttachments == 1,
2809 "function must have a single !dbg attachment", &F, I.second);
2810 CheckDI(isa<DISubprogram>(I.second),
2811 "function !dbg attachment must be a subprogram", &F, I.second);
2812 CheckDI(cast<DISubprogram>(I.second)->isDistinct(),
2813 "function definition may only have a distinct !dbg attachment",
2814 &F);
2815
2816 auto *SP = cast<DISubprogram>(I.second);
2817 const Function *&AttachedTo = DISubprogramAttachments[SP];
2818 CheckDI(!AttachedTo || AttachedTo == &F,
2819 "DISubprogram attached to more than one function", SP, &F);
2820 AttachedTo = &F;
2821 AllowLocs = AreDebugLocsAllowed::Yes;
2822 break;
2823 }
2824 case LLVMContext::MD_prof:
2825 ++NumProfAttachments;
2826 Check(NumProfAttachments == 1,
2827 "function must have a single !prof attachment", &F, I.second);
2828 break;
2829 case LLVMContext::MD_kcfi_type:
2830 ++NumKCFIAttachments;
2831 Check(NumKCFIAttachments == 1,
2832 "function must have a single !kcfi_type attachment", &F,
2833 I.second);
2834 break;
2835 }
2836
2837 // Verify the metadata itself.
2838 visitMDNode(*I.second, AllowLocs);
2839 }
2840 }
2841
2842 // If this function is actually an intrinsic, verify that it is only used in
2843 // direct call/invokes, never having its "address taken".
2844 // Only do this if the module is materialized, otherwise we don't have all the
2845 // uses.
2846 if (F.isIntrinsic() && F.getParent()->isMaterialized()) {
2847 const User *U;
2848 if (F.hasAddressTaken(&U, false, true, false,
2849 /*IgnoreARCAttachedCall=*/true))
2850 Check(false, "Invalid user of intrinsic instruction!", U);
2851 }
2852
2853 // Check intrinsics' signatures.
2854 switch (F.getIntrinsicID()) {
2855 case Intrinsic::experimental_gc_get_pointer_base: {
2856 FunctionType *FT = F.getFunctionType();
2857 Check(FT->getNumParams() == 1, "wrong number of parameters", F);
2858 Check(isa<PointerType>(F.getReturnType()),
2859 "gc.get.pointer.base must return a pointer", F);
2860 Check(FT->getParamType(0) == F.getReturnType(),
2861 "gc.get.pointer.base operand and result must be of the same type", F);
2862 break;
2863 }
2864 case Intrinsic::experimental_gc_get_pointer_offset: {
2865 FunctionType *FT = F.getFunctionType();
2866 Check(FT->getNumParams() == 1, "wrong number of parameters", F);
2867 Check(isa<PointerType>(FT->getParamType(0)),
2868 "gc.get.pointer.offset operand must be a pointer", F);
2869 Check(F.getReturnType()->isIntegerTy(),
2870 "gc.get.pointer.offset must return integer", F);
2871 break;
2872 }
2873 }
2874
2875 auto *N = F.getSubprogram();
2876 HasDebugInfo = (N != nullptr);
2877 if (!HasDebugInfo)
2878 return;
2879
2880 // Check that all !dbg attachments lead to back to N.
2881 //
2882 // FIXME: Check this incrementally while visiting !dbg attachments.
2883 // FIXME: Only check when N is the canonical subprogram for F.
2884 SmallPtrSet<const MDNode *, 32> Seen;
2885 auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
2886 // Be careful about using DILocation here since we might be dealing with
2887 // broken code (this is the Verifier after all).
2888 const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
2889 if (!DL)
2890 return;
2891 if (!Seen.insert(DL).second)
2892 return;
2893
2894 Metadata *Parent = DL->getRawScope();
2895 CheckDI(Parent && isa<DILocalScope>(Parent),
2896 "DILocation's scope must be a DILocalScope", N, &F, &I, DL, Parent);
2897
2898 DILocalScope *Scope = DL->getInlinedAtScope();
2899 Check(Scope, "Failed to find DILocalScope", DL);
2900
2901 if (!Seen.insert(Scope).second)
2902 return;
2903
2904 DISubprogram *SP = Scope->getSubprogram();
2905
2906 // Scope and SP could be the same MDNode and we don't want to skip
2907 // validation in that case
2908 if (SP && ((Scope != SP) && !Seen.insert(SP).second))
2909 return;
2910
2911 CheckDI(SP->describes(&F),
2912 "!dbg attachment points at wrong subprogram for function", N, &F,
2913 &I, DL, Scope, SP);
2914 };
2915 for (auto &BB : F)
2916 for (auto &I : BB) {
2917 VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
2918 // The llvm.loop annotations also contain two DILocations.
2919 if (auto MD = I.getMetadata(LLVMContext::MD_loop))
2920 for (unsigned i = 1; i < MD->getNumOperands(); ++i)
2921 VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
2922 if (BrokenDebugInfo)
2923 return;
2924 }
2925 }
2926
2927 // verifyBasicBlock - Verify that a basic block is well formed...
2928 //
visitBasicBlock(BasicBlock & BB)2929 void Verifier::visitBasicBlock(BasicBlock &BB) {
2930 InstsInThisBlock.clear();
2931 ConvergenceVerifyHelper.visit(BB);
2932
2933 // Ensure that basic blocks have terminators!
2934 Check(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
2935
2936 // Check constraints that this basic block imposes on all of the PHI nodes in
2937 // it.
2938 if (isa<PHINode>(BB.front())) {
2939 SmallVector<BasicBlock *, 8> Preds(predecessors(&BB));
2940 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
2941 llvm::sort(Preds);
2942 for (const PHINode &PN : BB.phis()) {
2943 Check(PN.getNumIncomingValues() == Preds.size(),
2944 "PHINode should have one entry for each predecessor of its "
2945 "parent basic block!",
2946 &PN);
2947
2948 // Get and sort all incoming values in the PHI node...
2949 Values.clear();
2950 Values.reserve(PN.getNumIncomingValues());
2951 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2952 Values.push_back(
2953 std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
2954 llvm::sort(Values);
2955
2956 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2957 // Check to make sure that if there is more than one entry for a
2958 // particular basic block in this PHI node, that the incoming values are
2959 // all identical.
2960 //
2961 Check(i == 0 || Values[i].first != Values[i - 1].first ||
2962 Values[i].second == Values[i - 1].second,
2963 "PHI node has multiple entries for the same basic block with "
2964 "different incoming values!",
2965 &PN, Values[i].first, Values[i].second, Values[i - 1].second);
2966
2967 // Check to make sure that the predecessors and PHI node entries are
2968 // matched up.
2969 Check(Values[i].first == Preds[i],
2970 "PHI node entries do not match predecessors!", &PN,
2971 Values[i].first, Preds[i]);
2972 }
2973 }
2974 }
2975
2976 // Check that all instructions have their parent pointers set up correctly.
2977 for (auto &I : BB)
2978 {
2979 Check(I.getParent() == &BB, "Instruction has bogus parent pointer!");
2980 }
2981
2982 // Confirm that no issues arise from the debug program.
2983 if (BB.IsNewDbgInfoFormat) {
2984 // Configure the validate function to not fire assertions, instead print
2985 // errors and return true if there's a problem.
2986 bool RetVal = BB.validateDbgValues(false, true, OS);
2987 Check(!RetVal, "Invalid configuration of new-debug-info data found");
2988 }
2989 }
2990
visitTerminator(Instruction & I)2991 void Verifier::visitTerminator(Instruction &I) {
2992 // Ensure that terminators only exist at the end of the basic block.
2993 Check(&I == I.getParent()->getTerminator(),
2994 "Terminator found in the middle of a basic block!", I.getParent());
2995 visitInstruction(I);
2996 }
2997
visitBranchInst(BranchInst & BI)2998 void Verifier::visitBranchInst(BranchInst &BI) {
2999 if (BI.isConditional()) {
3000 Check(BI.getCondition()->getType()->isIntegerTy(1),
3001 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
3002 }
3003 visitTerminator(BI);
3004 }
3005
visitReturnInst(ReturnInst & RI)3006 void Verifier::visitReturnInst(ReturnInst &RI) {
3007 Function *F = RI.getParent()->getParent();
3008 unsigned N = RI.getNumOperands();
3009 if (F->getReturnType()->isVoidTy())
3010 Check(N == 0,
3011 "Found return instr that returns non-void in Function of void "
3012 "return type!",
3013 &RI, F->getReturnType());
3014 else
3015 Check(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
3016 "Function return type does not match operand "
3017 "type of return inst!",
3018 &RI, F->getReturnType());
3019
3020 // Check to make sure that the return value has necessary properties for
3021 // terminators...
3022 visitTerminator(RI);
3023 }
3024
visitSwitchInst(SwitchInst & SI)3025 void Verifier::visitSwitchInst(SwitchInst &SI) {
3026 Check(SI.getType()->isVoidTy(), "Switch must have void result type!", &SI);
3027 // Check to make sure that all of the constants in the switch instruction
3028 // have the same type as the switched-on value.
3029 Type *SwitchTy = SI.getCondition()->getType();
3030 SmallPtrSet<ConstantInt*, 32> Constants;
3031 for (auto &Case : SI.cases()) {
3032 Check(isa<ConstantInt>(SI.getOperand(Case.getCaseIndex() * 2 + 2)),
3033 "Case value is not a constant integer.", &SI);
3034 Check(Case.getCaseValue()->getType() == SwitchTy,
3035 "Switch constants must all be same type as switch value!", &SI);
3036 Check(Constants.insert(Case.getCaseValue()).second,
3037 "Duplicate integer as switch case", &SI, Case.getCaseValue());
3038 }
3039
3040 visitTerminator(SI);
3041 }
3042
visitIndirectBrInst(IndirectBrInst & BI)3043 void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
3044 Check(BI.getAddress()->getType()->isPointerTy(),
3045 "Indirectbr operand must have pointer type!", &BI);
3046 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
3047 Check(BI.getDestination(i)->getType()->isLabelTy(),
3048 "Indirectbr destinations must all have pointer type!", &BI);
3049
3050 visitTerminator(BI);
3051 }
3052
visitCallBrInst(CallBrInst & CBI)3053 void Verifier::visitCallBrInst(CallBrInst &CBI) {
3054 Check(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!", &CBI);
3055 const InlineAsm *IA = cast<InlineAsm>(CBI.getCalledOperand());
3056 Check(!IA->canThrow(), "Unwinding from Callbr is not allowed");
3057
3058 verifyInlineAsmCall(CBI);
3059 visitTerminator(CBI);
3060 }
3061
visitSelectInst(SelectInst & SI)3062 void Verifier::visitSelectInst(SelectInst &SI) {
3063 Check(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
3064 SI.getOperand(2)),
3065 "Invalid operands for select instruction!", &SI);
3066
3067 Check(SI.getTrueValue()->getType() == SI.getType(),
3068 "Select values must have same type as select instruction!", &SI);
3069 visitInstruction(SI);
3070 }
3071
3072 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
3073 /// a pass, if any exist, it's an error.
3074 ///
visitUserOp1(Instruction & I)3075 void Verifier::visitUserOp1(Instruction &I) {
3076 Check(false, "User-defined operators should not live outside of a pass!", &I);
3077 }
3078
visitTruncInst(TruncInst & I)3079 void Verifier::visitTruncInst(TruncInst &I) {
3080 // Get the source and destination types
3081 Type *SrcTy = I.getOperand(0)->getType();
3082 Type *DestTy = I.getType();
3083
3084 // Get the size of the types in bits, we'll need this later
3085 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3086 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3087
3088 Check(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
3089 Check(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
3090 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3091 "trunc source and destination must both be a vector or neither", &I);
3092 Check(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
3093
3094 visitInstruction(I);
3095 }
3096
visitZExtInst(ZExtInst & I)3097 void Verifier::visitZExtInst(ZExtInst &I) {
3098 // Get the source and destination types
3099 Type *SrcTy = I.getOperand(0)->getType();
3100 Type *DestTy = I.getType();
3101
3102 // Get the size of the types in bits, we'll need this later
3103 Check(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
3104 Check(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
3105 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3106 "zext source and destination must both be a vector or neither", &I);
3107 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3108 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3109
3110 Check(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
3111
3112 visitInstruction(I);
3113 }
3114
visitSExtInst(SExtInst & I)3115 void Verifier::visitSExtInst(SExtInst &I) {
3116 // Get the source and destination types
3117 Type *SrcTy = I.getOperand(0)->getType();
3118 Type *DestTy = I.getType();
3119
3120 // Get the size of the types in bits, we'll need this later
3121 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3122 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3123
3124 Check(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
3125 Check(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
3126 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3127 "sext source and destination must both be a vector or neither", &I);
3128 Check(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
3129
3130 visitInstruction(I);
3131 }
3132
visitFPTruncInst(FPTruncInst & I)3133 void Verifier::visitFPTruncInst(FPTruncInst &I) {
3134 // Get the source and destination types
3135 Type *SrcTy = I.getOperand(0)->getType();
3136 Type *DestTy = I.getType();
3137 // Get the size of the types in bits, we'll need this later
3138 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3139 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3140
3141 Check(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
3142 Check(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
3143 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3144 "fptrunc source and destination must both be a vector or neither", &I);
3145 Check(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
3146
3147 visitInstruction(I);
3148 }
3149
visitFPExtInst(FPExtInst & I)3150 void Verifier::visitFPExtInst(FPExtInst &I) {
3151 // Get the source and destination types
3152 Type *SrcTy = I.getOperand(0)->getType();
3153 Type *DestTy = I.getType();
3154
3155 // Get the size of the types in bits, we'll need this later
3156 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3157 unsigned DestBitSize = DestTy->getScalarSizeInBits();
3158
3159 Check(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
3160 Check(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
3161 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(),
3162 "fpext source and destination must both be a vector or neither", &I);
3163 Check(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
3164
3165 visitInstruction(I);
3166 }
3167
visitUIToFPInst(UIToFPInst & I)3168 void Verifier::visitUIToFPInst(UIToFPInst &I) {
3169 // Get the source and destination types
3170 Type *SrcTy = I.getOperand(0)->getType();
3171 Type *DestTy = I.getType();
3172
3173 bool SrcVec = SrcTy->isVectorTy();
3174 bool DstVec = DestTy->isVectorTy();
3175
3176 Check(SrcVec == DstVec,
3177 "UIToFP source and dest must both be vector or scalar", &I);
3178 Check(SrcTy->isIntOrIntVectorTy(),
3179 "UIToFP source must be integer or integer vector", &I);
3180 Check(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
3181 &I);
3182
3183 if (SrcVec && DstVec)
3184 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3185 cast<VectorType>(DestTy)->getElementCount(),
3186 "UIToFP source and dest vector length mismatch", &I);
3187
3188 visitInstruction(I);
3189 }
3190
visitSIToFPInst(SIToFPInst & I)3191 void Verifier::visitSIToFPInst(SIToFPInst &I) {
3192 // Get the source and destination types
3193 Type *SrcTy = I.getOperand(0)->getType();
3194 Type *DestTy = I.getType();
3195
3196 bool SrcVec = SrcTy->isVectorTy();
3197 bool DstVec = DestTy->isVectorTy();
3198
3199 Check(SrcVec == DstVec,
3200 "SIToFP source and dest must both be vector or scalar", &I);
3201 Check(SrcTy->isIntOrIntVectorTy(),
3202 "SIToFP source must be integer or integer vector", &I);
3203 Check(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
3204 &I);
3205
3206 if (SrcVec && DstVec)
3207 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3208 cast<VectorType>(DestTy)->getElementCount(),
3209 "SIToFP source and dest vector length mismatch", &I);
3210
3211 visitInstruction(I);
3212 }
3213
visitFPToUIInst(FPToUIInst & I)3214 void Verifier::visitFPToUIInst(FPToUIInst &I) {
3215 // Get the source and destination types
3216 Type *SrcTy = I.getOperand(0)->getType();
3217 Type *DestTy = I.getType();
3218
3219 bool SrcVec = SrcTy->isVectorTy();
3220 bool DstVec = DestTy->isVectorTy();
3221
3222 Check(SrcVec == DstVec,
3223 "FPToUI source and dest must both be vector or scalar", &I);
3224 Check(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector", &I);
3225 Check(DestTy->isIntOrIntVectorTy(),
3226 "FPToUI result must be integer or integer vector", &I);
3227
3228 if (SrcVec && DstVec)
3229 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3230 cast<VectorType>(DestTy)->getElementCount(),
3231 "FPToUI source and dest vector length mismatch", &I);
3232
3233 visitInstruction(I);
3234 }
3235
visitFPToSIInst(FPToSIInst & I)3236 void Verifier::visitFPToSIInst(FPToSIInst &I) {
3237 // Get the source and destination types
3238 Type *SrcTy = I.getOperand(0)->getType();
3239 Type *DestTy = I.getType();
3240
3241 bool SrcVec = SrcTy->isVectorTy();
3242 bool DstVec = DestTy->isVectorTy();
3243
3244 Check(SrcVec == DstVec,
3245 "FPToSI source and dest must both be vector or scalar", &I);
3246 Check(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector", &I);
3247 Check(DestTy->isIntOrIntVectorTy(),
3248 "FPToSI result must be integer or integer vector", &I);
3249
3250 if (SrcVec && DstVec)
3251 Check(cast<VectorType>(SrcTy)->getElementCount() ==
3252 cast<VectorType>(DestTy)->getElementCount(),
3253 "FPToSI source and dest vector length mismatch", &I);
3254
3255 visitInstruction(I);
3256 }
3257
visitPtrToIntInst(PtrToIntInst & I)3258 void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
3259 // Get the source and destination types
3260 Type *SrcTy = I.getOperand(0)->getType();
3261 Type *DestTy = I.getType();
3262
3263 Check(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
3264
3265 Check(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
3266 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
3267 &I);
3268
3269 if (SrcTy->isVectorTy()) {
3270 auto *VSrc = cast<VectorType>(SrcTy);
3271 auto *VDest = cast<VectorType>(DestTy);
3272 Check(VSrc->getElementCount() == VDest->getElementCount(),
3273 "PtrToInt Vector width mismatch", &I);
3274 }
3275
3276 visitInstruction(I);
3277 }
3278
visitIntToPtrInst(IntToPtrInst & I)3279 void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
3280 // Get the source and destination types
3281 Type *SrcTy = I.getOperand(0)->getType();
3282 Type *DestTy = I.getType();
3283
3284 Check(SrcTy->isIntOrIntVectorTy(), "IntToPtr source must be an integral", &I);
3285 Check(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
3286
3287 Check(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
3288 &I);
3289 if (SrcTy->isVectorTy()) {
3290 auto *VSrc = cast<VectorType>(SrcTy);
3291 auto *VDest = cast<VectorType>(DestTy);
3292 Check(VSrc->getElementCount() == VDest->getElementCount(),
3293 "IntToPtr Vector width mismatch", &I);
3294 }
3295 visitInstruction(I);
3296 }
3297
visitBitCastInst(BitCastInst & I)3298 void Verifier::visitBitCastInst(BitCastInst &I) {
3299 Check(
3300 CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
3301 "Invalid bitcast", &I);
3302 visitInstruction(I);
3303 }
3304
visitAddrSpaceCastInst(AddrSpaceCastInst & I)3305 void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
3306 Type *SrcTy = I.getOperand(0)->getType();
3307 Type *DestTy = I.getType();
3308
3309 Check(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
3310 &I);
3311 Check(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
3312 &I);
3313 Check(SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace(),
3314 "AddrSpaceCast must be between different address spaces", &I);
3315 if (auto *SrcVTy = dyn_cast<VectorType>(SrcTy))
3316 Check(SrcVTy->getElementCount() ==
3317 cast<VectorType>(DestTy)->getElementCount(),
3318 "AddrSpaceCast vector pointer number of elements mismatch", &I);
3319 visitInstruction(I);
3320 }
3321
3322 /// visitPHINode - Ensure that a PHI node is well formed.
3323 ///
visitPHINode(PHINode & PN)3324 void Verifier::visitPHINode(PHINode &PN) {
3325 // Ensure that the PHI nodes are all grouped together at the top of the block.
3326 // This can be tested by checking whether the instruction before this is
3327 // either nonexistent (because this is begin()) or is a PHI node. If not,
3328 // then there is some other instruction before a PHI.
3329 Check(&PN == &PN.getParent()->front() ||
3330 isa<PHINode>(--BasicBlock::iterator(&PN)),
3331 "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
3332
3333 // Check that a PHI doesn't yield a Token.
3334 Check(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
3335
3336 // Check that all of the values of the PHI node have the same type as the
3337 // result, and that the incoming blocks are really basic blocks.
3338 for (Value *IncValue : PN.incoming_values()) {
3339 Check(PN.getType() == IncValue->getType(),
3340 "PHI node operands are not the same type as the result!", &PN);
3341 }
3342
3343 // All other PHI node constraints are checked in the visitBasicBlock method.
3344
3345 visitInstruction(PN);
3346 }
3347
visitCallBase(CallBase & Call)3348 void Verifier::visitCallBase(CallBase &Call) {
3349 Check(Call.getCalledOperand()->getType()->isPointerTy(),
3350 "Called function must be a pointer!", Call);
3351 FunctionType *FTy = Call.getFunctionType();
3352
3353 // Verify that the correct number of arguments are being passed
3354 if (FTy->isVarArg())
3355 Check(Call.arg_size() >= FTy->getNumParams(),
3356 "Called function requires more parameters than were provided!", Call);
3357 else
3358 Check(Call.arg_size() == FTy->getNumParams(),
3359 "Incorrect number of arguments passed to called function!", Call);
3360
3361 // Verify that all arguments to the call match the function type.
3362 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3363 Check(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
3364 "Call parameter type does not match function signature!",
3365 Call.getArgOperand(i), FTy->getParamType(i), Call);
3366
3367 AttributeList Attrs = Call.getAttributes();
3368
3369 Check(verifyAttributeCount(Attrs, Call.arg_size()),
3370 "Attribute after last parameter!", Call);
3371
3372 Function *Callee =
3373 dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
3374 bool IsIntrinsic = Callee && Callee->isIntrinsic();
3375 if (IsIntrinsic)
3376 Check(Callee->getValueType() == FTy,
3377 "Intrinsic called with incompatible signature", Call);
3378
3379 // Disallow calls to functions with the amdgpu_cs_chain[_preserve] calling
3380 // convention.
3381 auto CC = Call.getCallingConv();
3382 Check(CC != CallingConv::AMDGPU_CS_Chain &&
3383 CC != CallingConv::AMDGPU_CS_ChainPreserve,
3384 "Direct calls to amdgpu_cs_chain/amdgpu_cs_chain_preserve functions "
3385 "not allowed. Please use the @llvm.amdgpu.cs.chain intrinsic instead.",
3386 Call);
3387
3388 auto VerifyTypeAlign = [&](Type *Ty, const Twine &Message) {
3389 if (!Ty->isSized())
3390 return;
3391 Align ABIAlign = DL.getABITypeAlign(Ty);
3392 Align MaxAlign(ParamMaxAlignment);
3393 Check(ABIAlign <= MaxAlign,
3394 "Incorrect alignment of " + Message + " to called function!", Call);
3395 };
3396
3397 if (!IsIntrinsic) {
3398 VerifyTypeAlign(FTy->getReturnType(), "return type");
3399 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3400 Type *Ty = FTy->getParamType(i);
3401 VerifyTypeAlign(Ty, "argument passed");
3402 }
3403 }
3404
3405 if (Attrs.hasFnAttr(Attribute::Speculatable)) {
3406 // Don't allow speculatable on call sites, unless the underlying function
3407 // declaration is also speculatable.
3408 Check(Callee && Callee->isSpeculatable(),
3409 "speculatable attribute may not apply to call sites", Call);
3410 }
3411
3412 if (Attrs.hasFnAttr(Attribute::Preallocated)) {
3413 Check(Call.getCalledFunction()->getIntrinsicID() ==
3414 Intrinsic::call_preallocated_arg,
3415 "preallocated as a call site attribute can only be on "
3416 "llvm.call.preallocated.arg");
3417 }
3418
3419 // Verify call attributes.
3420 verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic, Call.isInlineAsm());
3421
3422 // Conservatively check the inalloca argument.
3423 // We have a bug if we can find that there is an underlying alloca without
3424 // inalloca.
3425 if (Call.hasInAllocaArgument()) {
3426 Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
3427 if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
3428 Check(AI->isUsedWithInAlloca(),
3429 "inalloca argument for call has mismatched alloca", AI, Call);
3430 }
3431
3432 // For each argument of the callsite, if it has the swifterror argument,
3433 // make sure the underlying alloca/parameter it comes from has a swifterror as
3434 // well.
3435 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3436 if (Call.paramHasAttr(i, Attribute::SwiftError)) {
3437 Value *SwiftErrorArg = Call.getArgOperand(i);
3438 if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
3439 Check(AI->isSwiftError(),
3440 "swifterror argument for call has mismatched alloca", AI, Call);
3441 continue;
3442 }
3443 auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
3444 Check(ArgI, "swifterror argument should come from an alloca or parameter",
3445 SwiftErrorArg, Call);
3446 Check(ArgI->hasSwiftErrorAttr(),
3447 "swifterror argument for call has mismatched parameter", ArgI,
3448 Call);
3449 }
3450
3451 if (Attrs.hasParamAttr(i, Attribute::ImmArg)) {
3452 // Don't allow immarg on call sites, unless the underlying declaration
3453 // also has the matching immarg.
3454 Check(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
3455 "immarg may not apply only to call sites", Call.getArgOperand(i),
3456 Call);
3457 }
3458
3459 if (Call.paramHasAttr(i, Attribute::ImmArg)) {
3460 Value *ArgVal = Call.getArgOperand(i);
3461 Check(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
3462 "immarg operand has non-immediate parameter", ArgVal, Call);
3463 }
3464
3465 if (Call.paramHasAttr(i, Attribute::Preallocated)) {
3466 Value *ArgVal = Call.getArgOperand(i);
3467 bool hasOB =
3468 Call.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0;
3469 bool isMustTail = Call.isMustTailCall();
3470 Check(hasOB != isMustTail,
3471 "preallocated operand either requires a preallocated bundle or "
3472 "the call to be musttail (but not both)",
3473 ArgVal, Call);
3474 }
3475 }
3476
3477 if (FTy->isVarArg()) {
3478 // FIXME? is 'nest' even legal here?
3479 bool SawNest = false;
3480 bool SawReturned = false;
3481
3482 for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
3483 if (Attrs.hasParamAttr(Idx, Attribute::Nest))
3484 SawNest = true;
3485 if (Attrs.hasParamAttr(Idx, Attribute::Returned))
3486 SawReturned = true;
3487 }
3488
3489 // Check attributes on the varargs part.
3490 for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
3491 Type *Ty = Call.getArgOperand(Idx)->getType();
3492 AttributeSet ArgAttrs = Attrs.getParamAttrs(Idx);
3493 verifyParameterAttrs(ArgAttrs, Ty, &Call);
3494
3495 if (ArgAttrs.hasAttribute(Attribute::Nest)) {
3496 Check(!SawNest, "More than one parameter has attribute nest!", Call);
3497 SawNest = true;
3498 }
3499
3500 if (ArgAttrs.hasAttribute(Attribute::Returned)) {
3501 Check(!SawReturned, "More than one parameter has attribute returned!",
3502 Call);
3503 Check(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
3504 "Incompatible argument and return types for 'returned' "
3505 "attribute",
3506 Call);
3507 SawReturned = true;
3508 }
3509
3510 // Statepoint intrinsic is vararg but the wrapped function may be not.
3511 // Allow sret here and check the wrapped function in verifyStatepoint.
3512 if (!Call.getCalledFunction() ||
3513 Call.getCalledFunction()->getIntrinsicID() !=
3514 Intrinsic::experimental_gc_statepoint)
3515 Check(!ArgAttrs.hasAttribute(Attribute::StructRet),
3516 "Attribute 'sret' cannot be used for vararg call arguments!",
3517 Call);
3518
3519 if (ArgAttrs.hasAttribute(Attribute::InAlloca))
3520 Check(Idx == Call.arg_size() - 1,
3521 "inalloca isn't on the last argument!", Call);
3522 }
3523 }
3524
3525 // Verify that there's no metadata unless it's a direct call to an intrinsic.
3526 if (!IsIntrinsic) {
3527 for (Type *ParamTy : FTy->params()) {
3528 Check(!ParamTy->isMetadataTy(),
3529 "Function has metadata parameter but isn't an intrinsic", Call);
3530 Check(!ParamTy->isTokenTy(),
3531 "Function has token parameter but isn't an intrinsic", Call);
3532 }
3533 }
3534
3535 // Verify that indirect calls don't return tokens.
3536 if (!Call.getCalledFunction()) {
3537 Check(!FTy->getReturnType()->isTokenTy(),
3538 "Return type cannot be token for indirect call!");
3539 Check(!FTy->getReturnType()->isX86_AMXTy(),
3540 "Return type cannot be x86_amx for indirect call!");
3541 }
3542
3543 if (Function *F = Call.getCalledFunction())
3544 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
3545 visitIntrinsicCall(ID, Call);
3546
3547 // Verify that a callsite has at most one "deopt", at most one "funclet", at
3548 // most one "gc-transition", at most one "cfguardtarget", at most one
3549 // "preallocated" operand bundle, and at most one "ptrauth" operand bundle.
3550 bool FoundDeoptBundle = false, FoundFuncletBundle = false,
3551 FoundGCTransitionBundle = false, FoundCFGuardTargetBundle = false,
3552 FoundPreallocatedBundle = false, FoundGCLiveBundle = false,
3553 FoundPtrauthBundle = false, FoundKCFIBundle = false,
3554 FoundAttachedCallBundle = false;
3555 for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
3556 OperandBundleUse BU = Call.getOperandBundleAt(i);
3557 uint32_t Tag = BU.getTagID();
3558 if (Tag == LLVMContext::OB_deopt) {
3559 Check(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
3560 FoundDeoptBundle = true;
3561 } else if (Tag == LLVMContext::OB_gc_transition) {
3562 Check(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
3563 Call);
3564 FoundGCTransitionBundle = true;
3565 } else if (Tag == LLVMContext::OB_funclet) {
3566 Check(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
3567 FoundFuncletBundle = true;
3568 Check(BU.Inputs.size() == 1,
3569 "Expected exactly one funclet bundle operand", Call);
3570 Check(isa<FuncletPadInst>(BU.Inputs.front()),
3571 "Funclet bundle operands should correspond to a FuncletPadInst",
3572 Call);
3573 } else if (Tag == LLVMContext::OB_cfguardtarget) {
3574 Check(!FoundCFGuardTargetBundle, "Multiple CFGuardTarget operand bundles",
3575 Call);
3576 FoundCFGuardTargetBundle = true;
3577 Check(BU.Inputs.size() == 1,
3578 "Expected exactly one cfguardtarget bundle operand", Call);
3579 } else if (Tag == LLVMContext::OB_ptrauth) {
3580 Check(!FoundPtrauthBundle, "Multiple ptrauth operand bundles", Call);
3581 FoundPtrauthBundle = true;
3582 Check(BU.Inputs.size() == 2,
3583 "Expected exactly two ptrauth bundle operands", Call);
3584 Check(isa<ConstantInt>(BU.Inputs[0]) &&
3585 BU.Inputs[0]->getType()->isIntegerTy(32),
3586 "Ptrauth bundle key operand must be an i32 constant", Call);
3587 Check(BU.Inputs[1]->getType()->isIntegerTy(64),
3588 "Ptrauth bundle discriminator operand must be an i64", Call);
3589 } else if (Tag == LLVMContext::OB_kcfi) {
3590 Check(!FoundKCFIBundle, "Multiple kcfi operand bundles", Call);
3591 FoundKCFIBundle = true;
3592 Check(BU.Inputs.size() == 1, "Expected exactly one kcfi bundle operand",
3593 Call);
3594 Check(isa<ConstantInt>(BU.Inputs[0]) &&
3595 BU.Inputs[0]->getType()->isIntegerTy(32),
3596 "Kcfi bundle operand must be an i32 constant", Call);
3597 } else if (Tag == LLVMContext::OB_preallocated) {
3598 Check(!FoundPreallocatedBundle, "Multiple preallocated operand bundles",
3599 Call);
3600 FoundPreallocatedBundle = true;
3601 Check(BU.Inputs.size() == 1,
3602 "Expected exactly one preallocated bundle operand", Call);
3603 auto Input = dyn_cast<IntrinsicInst>(BU.Inputs.front());
3604 Check(Input &&
3605 Input->getIntrinsicID() == Intrinsic::call_preallocated_setup,
3606 "\"preallocated\" argument must be a token from "
3607 "llvm.call.preallocated.setup",
3608 Call);
3609 } else if (Tag == LLVMContext::OB_gc_live) {
3610 Check(!FoundGCLiveBundle, "Multiple gc-live operand bundles", Call);
3611 FoundGCLiveBundle = true;
3612 } else if (Tag == LLVMContext::OB_clang_arc_attachedcall) {
3613 Check(!FoundAttachedCallBundle,
3614 "Multiple \"clang.arc.attachedcall\" operand bundles", Call);
3615 FoundAttachedCallBundle = true;
3616 verifyAttachedCallBundle(Call, BU);
3617 }
3618 }
3619
3620 // Verify that callee and callsite agree on whether to use pointer auth.
3621 Check(!(Call.getCalledFunction() && FoundPtrauthBundle),
3622 "Direct call cannot have a ptrauth bundle", Call);
3623
3624 // Verify that each inlinable callsite of a debug-info-bearing function in a
3625 // debug-info-bearing function has a debug location attached to it. Failure to
3626 // do so causes assertion failures when the inliner sets up inline scope info
3627 // (Interposable functions are not inlinable, neither are functions without
3628 // definitions.)
3629 if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
3630 !Call.getCalledFunction()->isInterposable() &&
3631 !Call.getCalledFunction()->isDeclaration() &&
3632 Call.getCalledFunction()->getSubprogram())
3633 CheckDI(Call.getDebugLoc(),
3634 "inlinable function call in a function with "
3635 "debug info must have a !dbg location",
3636 Call);
3637
3638 if (Call.isInlineAsm())
3639 verifyInlineAsmCall(Call);
3640
3641 ConvergenceVerifyHelper.visit(Call);
3642
3643 visitInstruction(Call);
3644 }
3645
verifyTailCCMustTailAttrs(const AttrBuilder & Attrs,StringRef Context)3646 void Verifier::verifyTailCCMustTailAttrs(const AttrBuilder &Attrs,
3647 StringRef Context) {
3648 Check(!Attrs.contains(Attribute::InAlloca),
3649 Twine("inalloca attribute not allowed in ") + Context);
3650 Check(!Attrs.contains(Attribute::InReg),
3651 Twine("inreg attribute not allowed in ") + Context);
3652 Check(!Attrs.contains(Attribute::SwiftError),
3653 Twine("swifterror attribute not allowed in ") + Context);
3654 Check(!Attrs.contains(Attribute::Preallocated),
3655 Twine("preallocated attribute not allowed in ") + Context);
3656 Check(!Attrs.contains(Attribute::ByRef),
3657 Twine("byref attribute not allowed in ") + Context);
3658 }
3659
3660 /// Two types are "congruent" if they are identical, or if they are both pointer
3661 /// types with different pointee types and the same address space.
isTypeCongruent(Type * L,Type * R)3662 static bool isTypeCongruent(Type *L, Type *R) {
3663 if (L == R)
3664 return true;
3665 PointerType *PL = dyn_cast<PointerType>(L);
3666 PointerType *PR = dyn_cast<PointerType>(R);
3667 if (!PL || !PR)
3668 return false;
3669 return PL->getAddressSpace() == PR->getAddressSpace();
3670 }
3671
getParameterABIAttributes(LLVMContext & C,unsigned I,AttributeList Attrs)3672 static AttrBuilder getParameterABIAttributes(LLVMContext& C, unsigned I, AttributeList Attrs) {
3673 static const Attribute::AttrKind ABIAttrs[] = {
3674 Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
3675 Attribute::InReg, Attribute::StackAlignment, Attribute::SwiftSelf,
3676 Attribute::SwiftAsync, Attribute::SwiftError, Attribute::Preallocated,
3677 Attribute::ByRef};
3678 AttrBuilder Copy(C);
3679 for (auto AK : ABIAttrs) {
3680 Attribute Attr = Attrs.getParamAttrs(I).getAttribute(AK);
3681 if (Attr.isValid())
3682 Copy.addAttribute(Attr);
3683 }
3684
3685 // `align` is ABI-affecting only in combination with `byval` or `byref`.
3686 if (Attrs.hasParamAttr(I, Attribute::Alignment) &&
3687 (Attrs.hasParamAttr(I, Attribute::ByVal) ||
3688 Attrs.hasParamAttr(I, Attribute::ByRef)))
3689 Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
3690 return Copy;
3691 }
3692
verifyMustTailCall(CallInst & CI)3693 void Verifier::verifyMustTailCall(CallInst &CI) {
3694 Check(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
3695
3696 Function *F = CI.getParent()->getParent();
3697 FunctionType *CallerTy = F->getFunctionType();
3698 FunctionType *CalleeTy = CI.getFunctionType();
3699 Check(CallerTy->isVarArg() == CalleeTy->isVarArg(),
3700 "cannot guarantee tail call due to mismatched varargs", &CI);
3701 Check(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
3702 "cannot guarantee tail call due to mismatched return types", &CI);
3703
3704 // - The calling conventions of the caller and callee must match.
3705 Check(F->getCallingConv() == CI.getCallingConv(),
3706 "cannot guarantee tail call due to mismatched calling conv", &CI);
3707
3708 // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
3709 // or a pointer bitcast followed by a ret instruction.
3710 // - The ret instruction must return the (possibly bitcasted) value
3711 // produced by the call or void.
3712 Value *RetVal = &CI;
3713 Instruction *Next = CI.getNextNode();
3714
3715 // Handle the optional bitcast.
3716 if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
3717 Check(BI->getOperand(0) == RetVal,
3718 "bitcast following musttail call must use the call", BI);
3719 RetVal = BI;
3720 Next = BI->getNextNode();
3721 }
3722
3723 // Check the return.
3724 ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
3725 Check(Ret, "musttail call must precede a ret with an optional bitcast", &CI);
3726 Check(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal ||
3727 isa<UndefValue>(Ret->getReturnValue()),
3728 "musttail call result must be returned", Ret);
3729
3730 AttributeList CallerAttrs = F->getAttributes();
3731 AttributeList CalleeAttrs = CI.getAttributes();
3732 if (CI.getCallingConv() == CallingConv::SwiftTail ||
3733 CI.getCallingConv() == CallingConv::Tail) {
3734 StringRef CCName =
3735 CI.getCallingConv() == CallingConv::Tail ? "tailcc" : "swifttailcc";
3736
3737 // - Only sret, byval, swiftself, and swiftasync ABI-impacting attributes
3738 // are allowed in swifttailcc call
3739 for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3740 AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
3741 SmallString<32> Context{CCName, StringRef(" musttail caller")};
3742 verifyTailCCMustTailAttrs(ABIAttrs, Context);
3743 }
3744 for (unsigned I = 0, E = CalleeTy->getNumParams(); I != E; ++I) {
3745 AttrBuilder ABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
3746 SmallString<32> Context{CCName, StringRef(" musttail callee")};
3747 verifyTailCCMustTailAttrs(ABIAttrs, Context);
3748 }
3749 // - Varargs functions are not allowed
3750 Check(!CallerTy->isVarArg(), Twine("cannot guarantee ") + CCName +
3751 " tail call for varargs function");
3752 return;
3753 }
3754
3755 // - The caller and callee prototypes must match. Pointer types of
3756 // parameters or return types may differ in pointee type, but not
3757 // address space.
3758 if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
3759 Check(CallerTy->getNumParams() == CalleeTy->getNumParams(),
3760 "cannot guarantee tail call due to mismatched parameter counts", &CI);
3761 for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3762 Check(
3763 isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
3764 "cannot guarantee tail call due to mismatched parameter types", &CI);
3765 }
3766 }
3767
3768 // - All ABI-impacting function attributes, such as sret, byval, inreg,
3769 // returned, preallocated, and inalloca, must match.
3770 for (unsigned I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3771 AttrBuilder CallerABIAttrs = getParameterABIAttributes(F->getContext(), I, CallerAttrs);
3772 AttrBuilder CalleeABIAttrs = getParameterABIAttributes(F->getContext(), I, CalleeAttrs);
3773 Check(CallerABIAttrs == CalleeABIAttrs,
3774 "cannot guarantee tail call due to mismatched ABI impacting "
3775 "function attributes",
3776 &CI, CI.getOperand(I));
3777 }
3778 }
3779
visitCallInst(CallInst & CI)3780 void Verifier::visitCallInst(CallInst &CI) {
3781 visitCallBase(CI);
3782
3783 if (CI.isMustTailCall())
3784 verifyMustTailCall(CI);
3785 }
3786
visitInvokeInst(InvokeInst & II)3787 void Verifier::visitInvokeInst(InvokeInst &II) {
3788 visitCallBase(II);
3789
3790 // Verify that the first non-PHI instruction of the unwind destination is an
3791 // exception handling instruction.
3792 Check(
3793 II.getUnwindDest()->isEHPad(),
3794 "The unwind destination does not have an exception handling instruction!",
3795 &II);
3796
3797 visitTerminator(II);
3798 }
3799
3800 /// visitUnaryOperator - Check the argument to the unary operator.
3801 ///
visitUnaryOperator(UnaryOperator & U)3802 void Verifier::visitUnaryOperator(UnaryOperator &U) {
3803 Check(U.getType() == U.getOperand(0)->getType(),
3804 "Unary operators must have same type for"
3805 "operands and result!",
3806 &U);
3807
3808 switch (U.getOpcode()) {
3809 // Check that floating-point arithmetic operators are only used with
3810 // floating-point operands.
3811 case Instruction::FNeg:
3812 Check(U.getType()->isFPOrFPVectorTy(),
3813 "FNeg operator only works with float types!", &U);
3814 break;
3815 default:
3816 llvm_unreachable("Unknown UnaryOperator opcode!");
3817 }
3818
3819 visitInstruction(U);
3820 }
3821
3822 /// visitBinaryOperator - Check that both arguments to the binary operator are
3823 /// of the same type!
3824 ///
visitBinaryOperator(BinaryOperator & B)3825 void Verifier::visitBinaryOperator(BinaryOperator &B) {
3826 Check(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
3827 "Both operands to a binary operator are not of the same type!", &B);
3828
3829 switch (B.getOpcode()) {
3830 // Check that integer arithmetic operators are only used with
3831 // integral operands.
3832 case Instruction::Add:
3833 case Instruction::Sub:
3834 case Instruction::Mul:
3835 case Instruction::SDiv:
3836 case Instruction::UDiv:
3837 case Instruction::SRem:
3838 case Instruction::URem:
3839 Check(B.getType()->isIntOrIntVectorTy(),
3840 "Integer arithmetic operators only work with integral types!", &B);
3841 Check(B.getType() == B.getOperand(0)->getType(),
3842 "Integer arithmetic operators must have same type "
3843 "for operands and result!",
3844 &B);
3845 break;
3846 // Check that floating-point arithmetic operators are only used with
3847 // floating-point operands.
3848 case Instruction::FAdd:
3849 case Instruction::FSub:
3850 case Instruction::FMul:
3851 case Instruction::FDiv:
3852 case Instruction::FRem:
3853 Check(B.getType()->isFPOrFPVectorTy(),
3854 "Floating-point arithmetic operators only work with "
3855 "floating-point types!",
3856 &B);
3857 Check(B.getType() == B.getOperand(0)->getType(),
3858 "Floating-point arithmetic operators must have same type "
3859 "for operands and result!",
3860 &B);
3861 break;
3862 // Check that logical operators are only used with integral operands.
3863 case Instruction::And:
3864 case Instruction::Or:
3865 case Instruction::Xor:
3866 Check(B.getType()->isIntOrIntVectorTy(),
3867 "Logical operators only work with integral types!", &B);
3868 Check(B.getType() == B.getOperand(0)->getType(),
3869 "Logical operators must have same type for operands and result!", &B);
3870 break;
3871 case Instruction::Shl:
3872 case Instruction::LShr:
3873 case Instruction::AShr:
3874 Check(B.getType()->isIntOrIntVectorTy(),
3875 "Shifts only work with integral types!", &B);
3876 Check(B.getType() == B.getOperand(0)->getType(),
3877 "Shift return type must be same as operands!", &B);
3878 break;
3879 default:
3880 llvm_unreachable("Unknown BinaryOperator opcode!");
3881 }
3882
3883 visitInstruction(B);
3884 }
3885
visitICmpInst(ICmpInst & IC)3886 void Verifier::visitICmpInst(ICmpInst &IC) {
3887 // Check that the operands are the same type
3888 Type *Op0Ty = IC.getOperand(0)->getType();
3889 Type *Op1Ty = IC.getOperand(1)->getType();
3890 Check(Op0Ty == Op1Ty,
3891 "Both operands to ICmp instruction are not of the same type!", &IC);
3892 // Check that the operands are the right type
3893 Check(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
3894 "Invalid operand types for ICmp instruction", &IC);
3895 // Check that the predicate is valid.
3896 Check(IC.isIntPredicate(), "Invalid predicate in ICmp instruction!", &IC);
3897
3898 visitInstruction(IC);
3899 }
3900
visitFCmpInst(FCmpInst & FC)3901 void Verifier::visitFCmpInst(FCmpInst &FC) {
3902 // Check that the operands are the same type
3903 Type *Op0Ty = FC.getOperand(0)->getType();
3904 Type *Op1Ty = FC.getOperand(1)->getType();
3905 Check(Op0Ty == Op1Ty,
3906 "Both operands to FCmp instruction are not of the same type!", &FC);
3907 // Check that the operands are the right type
3908 Check(Op0Ty->isFPOrFPVectorTy(), "Invalid operand types for FCmp instruction",
3909 &FC);
3910 // Check that the predicate is valid.
3911 Check(FC.isFPPredicate(), "Invalid predicate in FCmp instruction!", &FC);
3912
3913 visitInstruction(FC);
3914 }
3915
visitExtractElementInst(ExtractElementInst & EI)3916 void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
3917 Check(ExtractElementInst::isValidOperands(EI.getOperand(0), EI.getOperand(1)),
3918 "Invalid extractelement operands!", &EI);
3919 visitInstruction(EI);
3920 }
3921
visitInsertElementInst(InsertElementInst & IE)3922 void Verifier::visitInsertElementInst(InsertElementInst &IE) {
3923 Check(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
3924 IE.getOperand(2)),
3925 "Invalid insertelement operands!", &IE);
3926 visitInstruction(IE);
3927 }
3928
visitShuffleVectorInst(ShuffleVectorInst & SV)3929 void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
3930 Check(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
3931 SV.getShuffleMask()),
3932 "Invalid shufflevector operands!", &SV);
3933 visitInstruction(SV);
3934 }
3935
visitGetElementPtrInst(GetElementPtrInst & GEP)3936 void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
3937 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
3938
3939 Check(isa<PointerType>(TargetTy),
3940 "GEP base pointer is not a vector or a vector of pointers", &GEP);
3941 Check(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
3942
3943 if (auto *STy = dyn_cast<StructType>(GEP.getSourceElementType())) {
3944 SmallPtrSet<Type *, 4> Visited;
3945 Check(!STy->containsScalableVectorType(&Visited),
3946 "getelementptr cannot target structure that contains scalable vector"
3947 "type",
3948 &GEP);
3949 }
3950
3951 SmallVector<Value *, 16> Idxs(GEP.indices());
3952 Check(
3953 all_of(Idxs, [](Value *V) { return V->getType()->isIntOrIntVectorTy(); }),
3954 "GEP indexes must be integers", &GEP);
3955 Type *ElTy =
3956 GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
3957 Check(ElTy, "Invalid indices for GEP pointer type!", &GEP);
3958
3959 Check(GEP.getType()->isPtrOrPtrVectorTy() &&
3960 GEP.getResultElementType() == ElTy,
3961 "GEP is not of right type for indices!", &GEP, ElTy);
3962
3963 if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) {
3964 // Additional checks for vector GEPs.
3965 ElementCount GEPWidth = GEPVTy->getElementCount();
3966 if (GEP.getPointerOperandType()->isVectorTy())
3967 Check(
3968 GEPWidth ==
3969 cast<VectorType>(GEP.getPointerOperandType())->getElementCount(),
3970 "Vector GEP result width doesn't match operand's", &GEP);
3971 for (Value *Idx : Idxs) {
3972 Type *IndexTy = Idx->getType();
3973 if (auto *IndexVTy = dyn_cast<VectorType>(IndexTy)) {
3974 ElementCount IndexWidth = IndexVTy->getElementCount();
3975 Check(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
3976 }
3977 Check(IndexTy->isIntOrIntVectorTy(),
3978 "All GEP indices should be of integer type");
3979 }
3980 }
3981
3982 if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
3983 Check(GEP.getAddressSpace() == PTy->getAddressSpace(),
3984 "GEP address space doesn't match type", &GEP);
3985 }
3986
3987 visitInstruction(GEP);
3988 }
3989
isContiguous(const ConstantRange & A,const ConstantRange & B)3990 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
3991 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
3992 }
3993
3994 /// Verify !range and !absolute_symbol metadata. These have the same
3995 /// restrictions, except !absolute_symbol allows the full set.
verifyRangeMetadata(const Value & I,const MDNode * Range,Type * Ty,bool IsAbsoluteSymbol)3996 void Verifier::verifyRangeMetadata(const Value &I, const MDNode *Range,
3997 Type *Ty, bool IsAbsoluteSymbol) {
3998 unsigned NumOperands = Range->getNumOperands();
3999 Check(NumOperands % 2 == 0, "Unfinished range!", Range);
4000 unsigned NumRanges = NumOperands / 2;
4001 Check(NumRanges >= 1, "It should have at least one range!", Range);
4002
4003 ConstantRange LastRange(1, true); // Dummy initial value
4004 for (unsigned i = 0; i < NumRanges; ++i) {
4005 ConstantInt *Low =
4006 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
4007 Check(Low, "The lower limit must be an integer!", Low);
4008 ConstantInt *High =
4009 mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
4010 Check(High, "The upper limit must be an integer!", High);
4011 Check(High->getType() == Low->getType() &&
4012 High->getType() == Ty->getScalarType(),
4013 "Range types must match instruction type!", &I);
4014
4015 APInt HighV = High->getValue();
4016 APInt LowV = Low->getValue();
4017
4018 // ConstantRange asserts if the ranges are the same except for the min/max
4019 // value. Leave the cases it tolerates for the empty range error below.
4020 Check(LowV != HighV || LowV.isMaxValue() || LowV.isMinValue(),
4021 "The upper and lower limits cannot be the same value", &I);
4022
4023 ConstantRange CurRange(LowV, HighV);
4024 Check(!CurRange.isEmptySet() && (IsAbsoluteSymbol || !CurRange.isFullSet()),
4025 "Range must not be empty!", Range);
4026 if (i != 0) {
4027 Check(CurRange.intersectWith(LastRange).isEmptySet(),
4028 "Intervals are overlapping", Range);
4029 Check(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
4030 Range);
4031 Check(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
4032 Range);
4033 }
4034 LastRange = ConstantRange(LowV, HighV);
4035 }
4036 if (NumRanges > 2) {
4037 APInt FirstLow =
4038 mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
4039 APInt FirstHigh =
4040 mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
4041 ConstantRange FirstRange(FirstLow, FirstHigh);
4042 Check(FirstRange.intersectWith(LastRange).isEmptySet(),
4043 "Intervals are overlapping", Range);
4044 Check(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
4045 Range);
4046 }
4047 }
4048
visitRangeMetadata(Instruction & I,MDNode * Range,Type * Ty)4049 void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
4050 assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
4051 "precondition violation");
4052 verifyRangeMetadata(I, Range, Ty, false);
4053 }
4054
checkAtomicMemAccessSize(Type * Ty,const Instruction * I)4055 void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
4056 unsigned Size = DL.getTypeSizeInBits(Ty);
4057 Check(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
4058 Check(!(Size & (Size - 1)),
4059 "atomic memory access' operand must have a power-of-two size", Ty, I);
4060 }
4061
visitLoadInst(LoadInst & LI)4062 void Verifier::visitLoadInst(LoadInst &LI) {
4063 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
4064 Check(PTy, "Load operand must be a pointer.", &LI);
4065 Type *ElTy = LI.getType();
4066 if (MaybeAlign A = LI.getAlign()) {
4067 Check(A->value() <= Value::MaximumAlignment,
4068 "huge alignment values are unsupported", &LI);
4069 }
4070 Check(ElTy->isSized(), "loading unsized types is not allowed", &LI);
4071 if (LI.isAtomic()) {
4072 Check(LI.getOrdering() != AtomicOrdering::Release &&
4073 LI.getOrdering() != AtomicOrdering::AcquireRelease,
4074 "Load cannot have Release ordering", &LI);
4075 Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
4076 "atomic load operand must have integer, pointer, or floating point "
4077 "type!",
4078 ElTy, &LI);
4079 checkAtomicMemAccessSize(ElTy, &LI);
4080 } else {
4081 Check(LI.getSyncScopeID() == SyncScope::System,
4082 "Non-atomic load cannot have SynchronizationScope specified", &LI);
4083 }
4084
4085 visitInstruction(LI);
4086 }
4087
visitStoreInst(StoreInst & SI)4088 void Verifier::visitStoreInst(StoreInst &SI) {
4089 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
4090 Check(PTy, "Store operand must be a pointer.", &SI);
4091 Type *ElTy = SI.getOperand(0)->getType();
4092 if (MaybeAlign A = SI.getAlign()) {
4093 Check(A->value() <= Value::MaximumAlignment,
4094 "huge alignment values are unsupported", &SI);
4095 }
4096 Check(ElTy->isSized(), "storing unsized types is not allowed", &SI);
4097 if (SI.isAtomic()) {
4098 Check(SI.getOrdering() != AtomicOrdering::Acquire &&
4099 SI.getOrdering() != AtomicOrdering::AcquireRelease,
4100 "Store cannot have Acquire ordering", &SI);
4101 Check(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
4102 "atomic store operand must have integer, pointer, or floating point "
4103 "type!",
4104 ElTy, &SI);
4105 checkAtomicMemAccessSize(ElTy, &SI);
4106 } else {
4107 Check(SI.getSyncScopeID() == SyncScope::System,
4108 "Non-atomic store cannot have SynchronizationScope specified", &SI);
4109 }
4110 visitInstruction(SI);
4111 }
4112
4113 /// Check that SwiftErrorVal is used as a swifterror argument in CS.
verifySwiftErrorCall(CallBase & Call,const Value * SwiftErrorVal)4114 void Verifier::verifySwiftErrorCall(CallBase &Call,
4115 const Value *SwiftErrorVal) {
4116 for (const auto &I : llvm::enumerate(Call.args())) {
4117 if (I.value() == SwiftErrorVal) {
4118 Check(Call.paramHasAttr(I.index(), Attribute::SwiftError),
4119 "swifterror value when used in a callsite should be marked "
4120 "with swifterror attribute",
4121 SwiftErrorVal, Call);
4122 }
4123 }
4124 }
4125
verifySwiftErrorValue(const Value * SwiftErrorVal)4126 void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
4127 // Check that swifterror value is only used by loads, stores, or as
4128 // a swifterror argument.
4129 for (const User *U : SwiftErrorVal->users()) {
4130 Check(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
4131 isa<InvokeInst>(U),
4132 "swifterror value can only be loaded and stored from, or "
4133 "as a swifterror argument!",
4134 SwiftErrorVal, U);
4135 // If it is used by a store, check it is the second operand.
4136 if (auto StoreI = dyn_cast<StoreInst>(U))
4137 Check(StoreI->getOperand(1) == SwiftErrorVal,
4138 "swifterror value should be the second operand when used "
4139 "by stores",
4140 SwiftErrorVal, U);
4141 if (auto *Call = dyn_cast<CallBase>(U))
4142 verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
4143 }
4144 }
4145
visitAllocaInst(AllocaInst & AI)4146 void Verifier::visitAllocaInst(AllocaInst &AI) {
4147 SmallPtrSet<Type*, 4> Visited;
4148 Check(AI.getAllocatedType()->isSized(&Visited),
4149 "Cannot allocate unsized type", &AI);
4150 Check(AI.getArraySize()->getType()->isIntegerTy(),
4151 "Alloca array size must have integer type", &AI);
4152 if (MaybeAlign A = AI.getAlign()) {
4153 Check(A->value() <= Value::MaximumAlignment,
4154 "huge alignment values are unsupported", &AI);
4155 }
4156
4157 if (AI.isSwiftError()) {
4158 Check(AI.getAllocatedType()->isPointerTy(),
4159 "swifterror alloca must have pointer type", &AI);
4160 Check(!AI.isArrayAllocation(),
4161 "swifterror alloca must not be array allocation", &AI);
4162 verifySwiftErrorValue(&AI);
4163 }
4164
4165 visitInstruction(AI);
4166 }
4167
visitAtomicCmpXchgInst(AtomicCmpXchgInst & CXI)4168 void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
4169 Type *ElTy = CXI.getOperand(1)->getType();
4170 Check(ElTy->isIntOrPtrTy(),
4171 "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
4172 checkAtomicMemAccessSize(ElTy, &CXI);
4173 visitInstruction(CXI);
4174 }
4175
visitAtomicRMWInst(AtomicRMWInst & RMWI)4176 void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
4177 Check(RMWI.getOrdering() != AtomicOrdering::Unordered,
4178 "atomicrmw instructions cannot be unordered.", &RMWI);
4179 auto Op = RMWI.getOperation();
4180 Type *ElTy = RMWI.getOperand(1)->getType();
4181 if (Op == AtomicRMWInst::Xchg) {
4182 Check(ElTy->isIntegerTy() || ElTy->isFloatingPointTy() ||
4183 ElTy->isPointerTy(),
4184 "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
4185 " operand must have integer or floating point type!",
4186 &RMWI, ElTy);
4187 } else if (AtomicRMWInst::isFPOperation(Op)) {
4188 Check(ElTy->isFloatingPointTy(),
4189 "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
4190 " operand must have floating point type!",
4191 &RMWI, ElTy);
4192 } else {
4193 Check(ElTy->isIntegerTy(),
4194 "atomicrmw " + AtomicRMWInst::getOperationName(Op) +
4195 " operand must have integer type!",
4196 &RMWI, ElTy);
4197 }
4198 checkAtomicMemAccessSize(ElTy, &RMWI);
4199 Check(AtomicRMWInst::FIRST_BINOP <= Op && Op <= AtomicRMWInst::LAST_BINOP,
4200 "Invalid binary operation!", &RMWI);
4201 visitInstruction(RMWI);
4202 }
4203
visitFenceInst(FenceInst & FI)4204 void Verifier::visitFenceInst(FenceInst &FI) {
4205 const AtomicOrdering Ordering = FI.getOrdering();
4206 Check(Ordering == AtomicOrdering::Acquire ||
4207 Ordering == AtomicOrdering::Release ||
4208 Ordering == AtomicOrdering::AcquireRelease ||
4209 Ordering == AtomicOrdering::SequentiallyConsistent,
4210 "fence instructions may only have acquire, release, acq_rel, or "
4211 "seq_cst ordering.",
4212 &FI);
4213 visitInstruction(FI);
4214 }
4215
visitExtractValueInst(ExtractValueInst & EVI)4216 void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
4217 Check(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
4218 EVI.getIndices()) == EVI.getType(),
4219 "Invalid ExtractValueInst operands!", &EVI);
4220
4221 visitInstruction(EVI);
4222 }
4223
visitInsertValueInst(InsertValueInst & IVI)4224 void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
4225 Check(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
4226 IVI.getIndices()) ==
4227 IVI.getOperand(1)->getType(),
4228 "Invalid InsertValueInst operands!", &IVI);
4229
4230 visitInstruction(IVI);
4231 }
4232
getParentPad(Value * EHPad)4233 static Value *getParentPad(Value *EHPad) {
4234 if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
4235 return FPI->getParentPad();
4236
4237 return cast<CatchSwitchInst>(EHPad)->getParentPad();
4238 }
4239
visitEHPadPredecessors(Instruction & I)4240 void Verifier::visitEHPadPredecessors(Instruction &I) {
4241 assert(I.isEHPad());
4242
4243 BasicBlock *BB = I.getParent();
4244 Function *F = BB->getParent();
4245
4246 Check(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
4247
4248 if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
4249 // The landingpad instruction defines its parent as a landing pad block. The
4250 // landing pad block may be branched to only by the unwind edge of an
4251 // invoke.
4252 for (BasicBlock *PredBB : predecessors(BB)) {
4253 const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
4254 Check(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
4255 "Block containing LandingPadInst must be jumped to "
4256 "only by the unwind edge of an invoke.",
4257 LPI);
4258 }
4259 return;
4260 }
4261 if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
4262 if (!pred_empty(BB))
4263 Check(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
4264 "Block containg CatchPadInst must be jumped to "
4265 "only by its catchswitch.",
4266 CPI);
4267 Check(BB != CPI->getCatchSwitch()->getUnwindDest(),
4268 "Catchswitch cannot unwind to one of its catchpads",
4269 CPI->getCatchSwitch(), CPI);
4270 return;
4271 }
4272
4273 // Verify that each pred has a legal terminator with a legal to/from EH
4274 // pad relationship.
4275 Instruction *ToPad = &I;
4276 Value *ToPadParent = getParentPad(ToPad);
4277 for (BasicBlock *PredBB : predecessors(BB)) {
4278 Instruction *TI = PredBB->getTerminator();
4279 Value *FromPad;
4280 if (auto *II = dyn_cast<InvokeInst>(TI)) {
4281 Check(II->getUnwindDest() == BB && II->getNormalDest() != BB,
4282 "EH pad must be jumped to via an unwind edge", ToPad, II);
4283 if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
4284 FromPad = Bundle->Inputs[0];
4285 else
4286 FromPad = ConstantTokenNone::get(II->getContext());
4287 } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
4288 FromPad = CRI->getOperand(0);
4289 Check(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
4290 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
4291 FromPad = CSI;
4292 } else {
4293 Check(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
4294 }
4295
4296 // The edge may exit from zero or more nested pads.
4297 SmallSet<Value *, 8> Seen;
4298 for (;; FromPad = getParentPad(FromPad)) {
4299 Check(FromPad != ToPad,
4300 "EH pad cannot handle exceptions raised within it", FromPad, TI);
4301 if (FromPad == ToPadParent) {
4302 // This is a legal unwind edge.
4303 break;
4304 }
4305 Check(!isa<ConstantTokenNone>(FromPad),
4306 "A single unwind edge may only enter one EH pad", TI);
4307 Check(Seen.insert(FromPad).second, "EH pad jumps through a cycle of pads",
4308 FromPad);
4309
4310 // This will be diagnosed on the corresponding instruction already. We
4311 // need the extra check here to make sure getParentPad() works.
4312 Check(isa<FuncletPadInst>(FromPad) || isa<CatchSwitchInst>(FromPad),
4313 "Parent pad must be catchpad/cleanuppad/catchswitch", TI);
4314 }
4315 }
4316 }
4317
visitLandingPadInst(LandingPadInst & LPI)4318 void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
4319 // The landingpad instruction is ill-formed if it doesn't have any clauses and
4320 // isn't a cleanup.
4321 Check(LPI.getNumClauses() > 0 || LPI.isCleanup(),
4322 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
4323
4324 visitEHPadPredecessors(LPI);
4325
4326 if (!LandingPadResultTy)
4327 LandingPadResultTy = LPI.getType();
4328 else
4329 Check(LandingPadResultTy == LPI.getType(),
4330 "The landingpad instruction should have a consistent result type "
4331 "inside a function.",
4332 &LPI);
4333
4334 Function *F = LPI.getParent()->getParent();
4335 Check(F->hasPersonalityFn(),
4336 "LandingPadInst needs to be in a function with a personality.", &LPI);
4337
4338 // The landingpad instruction must be the first non-PHI instruction in the
4339 // block.
4340 Check(LPI.getParent()->getLandingPadInst() == &LPI,
4341 "LandingPadInst not the first non-PHI instruction in the block.", &LPI);
4342
4343 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
4344 Constant *Clause = LPI.getClause(i);
4345 if (LPI.isCatch(i)) {
4346 Check(isa<PointerType>(Clause->getType()),
4347 "Catch operand does not have pointer type!", &LPI);
4348 } else {
4349 Check(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
4350 Check(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
4351 "Filter operand is not an array of constants!", &LPI);
4352 }
4353 }
4354
4355 visitInstruction(LPI);
4356 }
4357
visitResumeInst(ResumeInst & RI)4358 void Verifier::visitResumeInst(ResumeInst &RI) {
4359 Check(RI.getFunction()->hasPersonalityFn(),
4360 "ResumeInst needs to be in a function with a personality.", &RI);
4361
4362 if (!LandingPadResultTy)
4363 LandingPadResultTy = RI.getValue()->getType();
4364 else
4365 Check(LandingPadResultTy == RI.getValue()->getType(),
4366 "The resume instruction should have a consistent result type "
4367 "inside a function.",
4368 &RI);
4369
4370 visitTerminator(RI);
4371 }
4372
visitCatchPadInst(CatchPadInst & CPI)4373 void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
4374 BasicBlock *BB = CPI.getParent();
4375
4376 Function *F = BB->getParent();
4377 Check(F->hasPersonalityFn(),
4378 "CatchPadInst needs to be in a function with a personality.", &CPI);
4379
4380 Check(isa<CatchSwitchInst>(CPI.getParentPad()),
4381 "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
4382 CPI.getParentPad());
4383
4384 // The catchpad instruction must be the first non-PHI instruction in the
4385 // block.
4386 Check(BB->getFirstNonPHI() == &CPI,
4387 "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
4388
4389 visitEHPadPredecessors(CPI);
4390 visitFuncletPadInst(CPI);
4391 }
4392
visitCatchReturnInst(CatchReturnInst & CatchReturn)4393 void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
4394 Check(isa<CatchPadInst>(CatchReturn.getOperand(0)),
4395 "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
4396 CatchReturn.getOperand(0));
4397
4398 visitTerminator(CatchReturn);
4399 }
4400
visitCleanupPadInst(CleanupPadInst & CPI)4401 void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
4402 BasicBlock *BB = CPI.getParent();
4403
4404 Function *F = BB->getParent();
4405 Check(F->hasPersonalityFn(),
4406 "CleanupPadInst needs to be in a function with a personality.", &CPI);
4407
4408 // The cleanuppad instruction must be the first non-PHI instruction in the
4409 // block.
4410 Check(BB->getFirstNonPHI() == &CPI,
4411 "CleanupPadInst not the first non-PHI instruction in the block.", &CPI);
4412
4413 auto *ParentPad = CPI.getParentPad();
4414 Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
4415 "CleanupPadInst has an invalid parent.", &CPI);
4416
4417 visitEHPadPredecessors(CPI);
4418 visitFuncletPadInst(CPI);
4419 }
4420
visitFuncletPadInst(FuncletPadInst & FPI)4421 void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
4422 User *FirstUser = nullptr;
4423 Value *FirstUnwindPad = nullptr;
4424 SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
4425 SmallSet<FuncletPadInst *, 8> Seen;
4426
4427 while (!Worklist.empty()) {
4428 FuncletPadInst *CurrentPad = Worklist.pop_back_val();
4429 Check(Seen.insert(CurrentPad).second,
4430 "FuncletPadInst must not be nested within itself", CurrentPad);
4431 Value *UnresolvedAncestorPad = nullptr;
4432 for (User *U : CurrentPad->users()) {
4433 BasicBlock *UnwindDest;
4434 if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
4435 UnwindDest = CRI->getUnwindDest();
4436 } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
4437 // We allow catchswitch unwind to caller to nest
4438 // within an outer pad that unwinds somewhere else,
4439 // because catchswitch doesn't have a nounwind variant.
4440 // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
4441 if (CSI->unwindsToCaller())
4442 continue;
4443 UnwindDest = CSI->getUnwindDest();
4444 } else if (auto *II = dyn_cast<InvokeInst>(U)) {
4445 UnwindDest = II->getUnwindDest();
4446 } else if (isa<CallInst>(U)) {
4447 // Calls which don't unwind may be found inside funclet
4448 // pads that unwind somewhere else. We don't *require*
4449 // such calls to be annotated nounwind.
4450 continue;
4451 } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
4452 // The unwind dest for a cleanup can only be found by
4453 // recursive search. Add it to the worklist, and we'll
4454 // search for its first use that determines where it unwinds.
4455 Worklist.push_back(CPI);
4456 continue;
4457 } else {
4458 Check(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
4459 continue;
4460 }
4461
4462 Value *UnwindPad;
4463 bool ExitsFPI;
4464 if (UnwindDest) {
4465 UnwindPad = UnwindDest->getFirstNonPHI();
4466 if (!cast<Instruction>(UnwindPad)->isEHPad())
4467 continue;
4468 Value *UnwindParent = getParentPad(UnwindPad);
4469 // Ignore unwind edges that don't exit CurrentPad.
4470 if (UnwindParent == CurrentPad)
4471 continue;
4472 // Determine whether the original funclet pad is exited,
4473 // and if we are scanning nested pads determine how many
4474 // of them are exited so we can stop searching their
4475 // children.
4476 Value *ExitedPad = CurrentPad;
4477 ExitsFPI = false;
4478 do {
4479 if (ExitedPad == &FPI) {
4480 ExitsFPI = true;
4481 // Now we can resolve any ancestors of CurrentPad up to
4482 // FPI, but not including FPI since we need to make sure
4483 // to check all direct users of FPI for consistency.
4484 UnresolvedAncestorPad = &FPI;
4485 break;
4486 }
4487 Value *ExitedParent = getParentPad(ExitedPad);
4488 if (ExitedParent == UnwindParent) {
4489 // ExitedPad is the ancestor-most pad which this unwind
4490 // edge exits, so we can resolve up to it, meaning that
4491 // ExitedParent is the first ancestor still unresolved.
4492 UnresolvedAncestorPad = ExitedParent;
4493 break;
4494 }
4495 ExitedPad = ExitedParent;
4496 } while (!isa<ConstantTokenNone>(ExitedPad));
4497 } else {
4498 // Unwinding to caller exits all pads.
4499 UnwindPad = ConstantTokenNone::get(FPI.getContext());
4500 ExitsFPI = true;
4501 UnresolvedAncestorPad = &FPI;
4502 }
4503
4504 if (ExitsFPI) {
4505 // This unwind edge exits FPI. Make sure it agrees with other
4506 // such edges.
4507 if (FirstUser) {
4508 Check(UnwindPad == FirstUnwindPad,
4509 "Unwind edges out of a funclet "
4510 "pad must have the same unwind "
4511 "dest",
4512 &FPI, U, FirstUser);
4513 } else {
4514 FirstUser = U;
4515 FirstUnwindPad = UnwindPad;
4516 // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
4517 if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
4518 getParentPad(UnwindPad) == getParentPad(&FPI))
4519 SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
4520 }
4521 }
4522 // Make sure we visit all uses of FPI, but for nested pads stop as
4523 // soon as we know where they unwind to.
4524 if (CurrentPad != &FPI)
4525 break;
4526 }
4527 if (UnresolvedAncestorPad) {
4528 if (CurrentPad == UnresolvedAncestorPad) {
4529 // When CurrentPad is FPI itself, we don't mark it as resolved even if
4530 // we've found an unwind edge that exits it, because we need to verify
4531 // all direct uses of FPI.
4532 assert(CurrentPad == &FPI);
4533 continue;
4534 }
4535 // Pop off the worklist any nested pads that we've found an unwind
4536 // destination for. The pads on the worklist are the uncles,
4537 // great-uncles, etc. of CurrentPad. We've found an unwind destination
4538 // for all ancestors of CurrentPad up to but not including
4539 // UnresolvedAncestorPad.
4540 Value *ResolvedPad = CurrentPad;
4541 while (!Worklist.empty()) {
4542 Value *UnclePad = Worklist.back();
4543 Value *AncestorPad = getParentPad(UnclePad);
4544 // Walk ResolvedPad up the ancestor list until we either find the
4545 // uncle's parent or the last resolved ancestor.
4546 while (ResolvedPad != AncestorPad) {
4547 Value *ResolvedParent = getParentPad(ResolvedPad);
4548 if (ResolvedParent == UnresolvedAncestorPad) {
4549 break;
4550 }
4551 ResolvedPad = ResolvedParent;
4552 }
4553 // If the resolved ancestor search didn't find the uncle's parent,
4554 // then the uncle is not yet resolved.
4555 if (ResolvedPad != AncestorPad)
4556 break;
4557 // This uncle is resolved, so pop it from the worklist.
4558 Worklist.pop_back();
4559 }
4560 }
4561 }
4562
4563 if (FirstUnwindPad) {
4564 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
4565 BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
4566 Value *SwitchUnwindPad;
4567 if (SwitchUnwindDest)
4568 SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
4569 else
4570 SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
4571 Check(SwitchUnwindPad == FirstUnwindPad,
4572 "Unwind edges out of a catch must have the same unwind dest as "
4573 "the parent catchswitch",
4574 &FPI, FirstUser, CatchSwitch);
4575 }
4576 }
4577
4578 visitInstruction(FPI);
4579 }
4580
visitCatchSwitchInst(CatchSwitchInst & CatchSwitch)4581 void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
4582 BasicBlock *BB = CatchSwitch.getParent();
4583
4584 Function *F = BB->getParent();
4585 Check(F->hasPersonalityFn(),
4586 "CatchSwitchInst needs to be in a function with a personality.",
4587 &CatchSwitch);
4588
4589 // The catchswitch instruction must be the first non-PHI instruction in the
4590 // block.
4591 Check(BB->getFirstNonPHI() == &CatchSwitch,
4592 "CatchSwitchInst not the first non-PHI instruction in the block.",
4593 &CatchSwitch);
4594
4595 auto *ParentPad = CatchSwitch.getParentPad();
4596 Check(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
4597 "CatchSwitchInst has an invalid parent.", ParentPad);
4598
4599 if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
4600 Instruction *I = UnwindDest->getFirstNonPHI();
4601 Check(I->isEHPad() && !isa<LandingPadInst>(I),
4602 "CatchSwitchInst must unwind to an EH block which is not a "
4603 "landingpad.",
4604 &CatchSwitch);
4605
4606 // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
4607 if (getParentPad(I) == ParentPad)
4608 SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
4609 }
4610
4611 Check(CatchSwitch.getNumHandlers() != 0,
4612 "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
4613
4614 for (BasicBlock *Handler : CatchSwitch.handlers()) {
4615 Check(isa<CatchPadInst>(Handler->getFirstNonPHI()),
4616 "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler);
4617 }
4618
4619 visitEHPadPredecessors(CatchSwitch);
4620 visitTerminator(CatchSwitch);
4621 }
4622
visitCleanupReturnInst(CleanupReturnInst & CRI)4623 void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
4624 Check(isa<CleanupPadInst>(CRI.getOperand(0)),
4625 "CleanupReturnInst needs to be provided a CleanupPad", &CRI,
4626 CRI.getOperand(0));
4627
4628 if (BasicBlock *UnwindDest = CRI.getUnwindDest()) {
4629 Instruction *I = UnwindDest->getFirstNonPHI();
4630 Check(I->isEHPad() && !isa<LandingPadInst>(I),
4631 "CleanupReturnInst must unwind to an EH block which is not a "
4632 "landingpad.",
4633 &CRI);
4634 }
4635
4636 visitTerminator(CRI);
4637 }
4638
verifyDominatesUse(Instruction & I,unsigned i)4639 void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
4640 Instruction *Op = cast<Instruction>(I.getOperand(i));
4641 // If the we have an invalid invoke, don't try to compute the dominance.
4642 // We already reject it in the invoke specific checks and the dominance
4643 // computation doesn't handle multiple edges.
4644 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
4645 if (II->getNormalDest() == II->getUnwindDest())
4646 return;
4647 }
4648
4649 // Quick check whether the def has already been encountered in the same block.
4650 // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
4651 // uses are defined to happen on the incoming edge, not at the instruction.
4652 //
4653 // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
4654 // wrapping an SSA value, assert that we've already encountered it. See
4655 // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
4656 if (!isa<PHINode>(I) && InstsInThisBlock.count(Op))
4657 return;
4658
4659 const Use &U = I.getOperandUse(i);
4660 Check(DT.dominates(Op, U), "Instruction does not dominate all uses!", Op, &I);
4661 }
4662
visitDereferenceableMetadata(Instruction & I,MDNode * MD)4663 void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
4664 Check(I.getType()->isPointerTy(),
4665 "dereferenceable, dereferenceable_or_null "
4666 "apply only to pointer types",
4667 &I);
4668 Check((isa<LoadInst>(I) || isa<IntToPtrInst>(I)),
4669 "dereferenceable, dereferenceable_or_null apply only to load"
4670 " and inttoptr instructions, use attributes for calls or invokes",
4671 &I);
4672 Check(MD->getNumOperands() == 1,
4673 "dereferenceable, dereferenceable_or_null "
4674 "take one operand!",
4675 &I);
4676 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0));
4677 Check(CI && CI->getType()->isIntegerTy(64),
4678 "dereferenceable, "
4679 "dereferenceable_or_null metadata value must be an i64!",
4680 &I);
4681 }
4682
visitProfMetadata(Instruction & I,MDNode * MD)4683 void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
4684 Check(MD->getNumOperands() >= 2,
4685 "!prof annotations should have no less than 2 operands", MD);
4686
4687 // Check first operand.
4688 Check(MD->getOperand(0) != nullptr, "first operand should not be null", MD);
4689 Check(isa<MDString>(MD->getOperand(0)),
4690 "expected string with name of the !prof annotation", MD);
4691 MDString *MDS = cast<MDString>(MD->getOperand(0));
4692 StringRef ProfName = MDS->getString();
4693
4694 // Check consistency of !prof branch_weights metadata.
4695 if (ProfName.equals("branch_weights")) {
4696 if (isa<InvokeInst>(&I)) {
4697 Check(MD->getNumOperands() == 2 || MD->getNumOperands() == 3,
4698 "Wrong number of InvokeInst branch_weights operands", MD);
4699 } else {
4700 unsigned ExpectedNumOperands = 0;
4701 if (BranchInst *BI = dyn_cast<BranchInst>(&I))
4702 ExpectedNumOperands = BI->getNumSuccessors();
4703 else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
4704 ExpectedNumOperands = SI->getNumSuccessors();
4705 else if (isa<CallInst>(&I))
4706 ExpectedNumOperands = 1;
4707 else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
4708 ExpectedNumOperands = IBI->getNumDestinations();
4709 else if (isa<SelectInst>(&I))
4710 ExpectedNumOperands = 2;
4711 else if (CallBrInst *CI = dyn_cast<CallBrInst>(&I))
4712 ExpectedNumOperands = CI->getNumSuccessors();
4713 else
4714 CheckFailed("!prof branch_weights are not allowed for this instruction",
4715 MD);
4716
4717 Check(MD->getNumOperands() == 1 + ExpectedNumOperands,
4718 "Wrong number of operands", MD);
4719 }
4720 for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
4721 auto &MDO = MD->getOperand(i);
4722 Check(MDO, "second operand should not be null", MD);
4723 Check(mdconst::dyn_extract<ConstantInt>(MDO),
4724 "!prof brunch_weights operand is not a const int");
4725 }
4726 }
4727 }
4728
visitDIAssignIDMetadata(Instruction & I,MDNode * MD)4729 void Verifier::visitDIAssignIDMetadata(Instruction &I, MDNode *MD) {
4730 assert(I.hasMetadata(LLVMContext::MD_DIAssignID));
4731 bool ExpectedInstTy =
4732 isa<AllocaInst>(I) || isa<StoreInst>(I) || isa<MemIntrinsic>(I);
4733 CheckDI(ExpectedInstTy, "!DIAssignID attached to unexpected instruction kind",
4734 I, MD);
4735 // Iterate over the MetadataAsValue uses of the DIAssignID - these should
4736 // only be found as DbgAssignIntrinsic operands.
4737 if (auto *AsValue = MetadataAsValue::getIfExists(Context, MD)) {
4738 for (auto *User : AsValue->users()) {
4739 CheckDI(isa<DbgAssignIntrinsic>(User),
4740 "!DIAssignID should only be used by llvm.dbg.assign intrinsics",
4741 MD, User);
4742 // All of the dbg.assign intrinsics should be in the same function as I.
4743 if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(User))
4744 CheckDI(DAI->getFunction() == I.getFunction(),
4745 "dbg.assign not in same function as inst", DAI, &I);
4746 }
4747 }
4748 for (DPValue *DPV : cast<DIAssignID>(MD)->getAllDPValueUsers()) {
4749 CheckDI(DPV->isDbgAssign(),
4750 "!DIAssignID should only be used by Assign DPVs.", MD, DPV);
4751 CheckDI(DPV->getFunction() == I.getFunction(),
4752 "DPVAssign not in same function as inst", DPV, &I);
4753 }
4754 }
4755
visitCallStackMetadata(MDNode * MD)4756 void Verifier::visitCallStackMetadata(MDNode *MD) {
4757 // Call stack metadata should consist of a list of at least 1 constant int
4758 // (representing a hash of the location).
4759 Check(MD->getNumOperands() >= 1,
4760 "call stack metadata should have at least 1 operand", MD);
4761
4762 for (const auto &Op : MD->operands())
4763 Check(mdconst::dyn_extract_or_null<ConstantInt>(Op),
4764 "call stack metadata operand should be constant integer", Op);
4765 }
4766
visitMemProfMetadata(Instruction & I,MDNode * MD)4767 void Verifier::visitMemProfMetadata(Instruction &I, MDNode *MD) {
4768 Check(isa<CallBase>(I), "!memprof metadata should only exist on calls", &I);
4769 Check(MD->getNumOperands() >= 1,
4770 "!memprof annotations should have at least 1 metadata operand "
4771 "(MemInfoBlock)",
4772 MD);
4773
4774 // Check each MIB
4775 for (auto &MIBOp : MD->operands()) {
4776 MDNode *MIB = dyn_cast<MDNode>(MIBOp);
4777 // The first operand of an MIB should be the call stack metadata.
4778 // There rest of the operands should be MDString tags, and there should be
4779 // at least one.
4780 Check(MIB->getNumOperands() >= 2,
4781 "Each !memprof MemInfoBlock should have at least 2 operands", MIB);
4782
4783 // Check call stack metadata (first operand).
4784 Check(MIB->getOperand(0) != nullptr,
4785 "!memprof MemInfoBlock first operand should not be null", MIB);
4786 Check(isa<MDNode>(MIB->getOperand(0)),
4787 "!memprof MemInfoBlock first operand should be an MDNode", MIB);
4788 MDNode *StackMD = dyn_cast<MDNode>(MIB->getOperand(0));
4789 visitCallStackMetadata(StackMD);
4790
4791 // Check that remaining operands are MDString.
4792 Check(llvm::all_of(llvm::drop_begin(MIB->operands()),
4793 [](const MDOperand &Op) { return isa<MDString>(Op); }),
4794 "Not all !memprof MemInfoBlock operands 1 to N are MDString", MIB);
4795 }
4796 }
4797
visitCallsiteMetadata(Instruction & I,MDNode * MD)4798 void Verifier::visitCallsiteMetadata(Instruction &I, MDNode *MD) {
4799 Check(isa<CallBase>(I), "!callsite metadata should only exist on calls", &I);
4800 // Verify the partial callstack annotated from memprof profiles. This callsite
4801 // is a part of a profiled allocation callstack.
4802 visitCallStackMetadata(MD);
4803 }
4804
visitAnnotationMetadata(MDNode * Annotation)4805 void Verifier::visitAnnotationMetadata(MDNode *Annotation) {
4806 Check(isa<MDTuple>(Annotation), "annotation must be a tuple");
4807 Check(Annotation->getNumOperands() >= 1,
4808 "annotation must have at least one operand");
4809 for (const MDOperand &Op : Annotation->operands()) {
4810 bool TupleOfStrings =
4811 isa<MDTuple>(Op.get()) &&
4812 all_of(cast<MDTuple>(Op)->operands(), [](auto &Annotation) {
4813 return isa<MDString>(Annotation.get());
4814 });
4815 Check(isa<MDString>(Op.get()) || TupleOfStrings,
4816 "operands must be a string or a tuple of strings");
4817 }
4818 }
4819
visitAliasScopeMetadata(const MDNode * MD)4820 void Verifier::visitAliasScopeMetadata(const MDNode *MD) {
4821 unsigned NumOps = MD->getNumOperands();
4822 Check(NumOps >= 2 && NumOps <= 3, "scope must have two or three operands",
4823 MD);
4824 Check(MD->getOperand(0).get() == MD || isa<MDString>(MD->getOperand(0)),
4825 "first scope operand must be self-referential or string", MD);
4826 if (NumOps == 3)
4827 Check(isa<MDString>(MD->getOperand(2)),
4828 "third scope operand must be string (if used)", MD);
4829
4830 MDNode *Domain = dyn_cast<MDNode>(MD->getOperand(1));
4831 Check(Domain != nullptr, "second scope operand must be MDNode", MD);
4832
4833 unsigned NumDomainOps = Domain->getNumOperands();
4834 Check(NumDomainOps >= 1 && NumDomainOps <= 2,
4835 "domain must have one or two operands", Domain);
4836 Check(Domain->getOperand(0).get() == Domain ||
4837 isa<MDString>(Domain->getOperand(0)),
4838 "first domain operand must be self-referential or string", Domain);
4839 if (NumDomainOps == 2)
4840 Check(isa<MDString>(Domain->getOperand(1)),
4841 "second domain operand must be string (if used)", Domain);
4842 }
4843
visitAliasScopeListMetadata(const MDNode * MD)4844 void Verifier::visitAliasScopeListMetadata(const MDNode *MD) {
4845 for (const MDOperand &Op : MD->operands()) {
4846 const MDNode *OpMD = dyn_cast<MDNode>(Op);
4847 Check(OpMD != nullptr, "scope list must consist of MDNodes", MD);
4848 visitAliasScopeMetadata(OpMD);
4849 }
4850 }
4851
visitAccessGroupMetadata(const MDNode * MD)4852 void Verifier::visitAccessGroupMetadata(const MDNode *MD) {
4853 auto IsValidAccessScope = [](const MDNode *MD) {
4854 return MD->getNumOperands() == 0 && MD->isDistinct();
4855 };
4856
4857 // It must be either an access scope itself...
4858 if (IsValidAccessScope(MD))
4859 return;
4860
4861 // ...or a list of access scopes.
4862 for (const MDOperand &Op : MD->operands()) {
4863 const MDNode *OpMD = dyn_cast<MDNode>(Op);
4864 Check(OpMD != nullptr, "Access scope list must consist of MDNodes", MD);
4865 Check(IsValidAccessScope(OpMD),
4866 "Access scope list contains invalid access scope", MD);
4867 }
4868 }
4869
4870 /// verifyInstruction - Verify that an instruction is well formed.
4871 ///
visitInstruction(Instruction & I)4872 void Verifier::visitInstruction(Instruction &I) {
4873 BasicBlock *BB = I.getParent();
4874 Check(BB, "Instruction not embedded in basic block!", &I);
4875
4876 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
4877 for (User *U : I.users()) {
4878 Check(U != (User *)&I || !DT.isReachableFromEntry(BB),
4879 "Only PHI nodes may reference their own value!", &I);
4880 }
4881 }
4882
4883 // Check that void typed values don't have names
4884 Check(!I.getType()->isVoidTy() || !I.hasName(),
4885 "Instruction has a name, but provides a void value!", &I);
4886
4887 // Check that the return value of the instruction is either void or a legal
4888 // value type.
4889 Check(I.getType()->isVoidTy() || I.getType()->isFirstClassType(),
4890 "Instruction returns a non-scalar type!", &I);
4891
4892 // Check that the instruction doesn't produce metadata. Calls are already
4893 // checked against the callee type.
4894 Check(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I),
4895 "Invalid use of metadata!", &I);
4896
4897 // Check that all uses of the instruction, if they are instructions
4898 // themselves, actually have parent basic blocks. If the use is not an
4899 // instruction, it is an error!
4900 for (Use &U : I.uses()) {
4901 if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
4902 Check(Used->getParent() != nullptr,
4903 "Instruction referencing"
4904 " instruction not embedded in a basic block!",
4905 &I, Used);
4906 else {
4907 CheckFailed("Use of instruction is not an instruction!", U);
4908 return;
4909 }
4910 }
4911
4912 // Get a pointer to the call base of the instruction if it is some form of
4913 // call.
4914 const CallBase *CBI = dyn_cast<CallBase>(&I);
4915
4916 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
4917 Check(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
4918
4919 // Check to make sure that only first-class-values are operands to
4920 // instructions.
4921 if (!I.getOperand(i)->getType()->isFirstClassType()) {
4922 Check(false, "Instruction operands must be first-class values!", &I);
4923 }
4924
4925 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
4926 // This code checks whether the function is used as the operand of a
4927 // clang_arc_attachedcall operand bundle.
4928 auto IsAttachedCallOperand = [](Function *F, const CallBase *CBI,
4929 int Idx) {
4930 return CBI && CBI->isOperandBundleOfType(
4931 LLVMContext::OB_clang_arc_attachedcall, Idx);
4932 };
4933
4934 // Check to make sure that the "address of" an intrinsic function is never
4935 // taken. Ignore cases where the address of the intrinsic function is used
4936 // as the argument of operand bundle "clang.arc.attachedcall" as those
4937 // cases are handled in verifyAttachedCallBundle.
4938 Check((!F->isIntrinsic() ||
4939 (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)) ||
4940 IsAttachedCallOperand(F, CBI, i)),
4941 "Cannot take the address of an intrinsic!", &I);
4942 Check(!F->isIntrinsic() || isa<CallInst>(I) ||
4943 F->getIntrinsicID() == Intrinsic::donothing ||
4944 F->getIntrinsicID() == Intrinsic::seh_try_begin ||
4945 F->getIntrinsicID() == Intrinsic::seh_try_end ||
4946 F->getIntrinsicID() == Intrinsic::seh_scope_begin ||
4947 F->getIntrinsicID() == Intrinsic::seh_scope_end ||
4948 F->getIntrinsicID() == Intrinsic::coro_resume ||
4949 F->getIntrinsicID() == Intrinsic::coro_destroy ||
4950 F->getIntrinsicID() ==
4951 Intrinsic::experimental_patchpoint_void ||
4952 F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
4953 F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
4954 F->getIntrinsicID() == Intrinsic::wasm_rethrow ||
4955 IsAttachedCallOperand(F, CBI, i),
4956 "Cannot invoke an intrinsic other than donothing, patchpoint, "
4957 "statepoint, coro_resume, coro_destroy or clang.arc.attachedcall",
4958 &I);
4959 Check(F->getParent() == &M, "Referencing function in another module!", &I,
4960 &M, F, F->getParent());
4961 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
4962 Check(OpBB->getParent() == BB->getParent(),
4963 "Referring to a basic block in another function!", &I);
4964 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
4965 Check(OpArg->getParent() == BB->getParent(),
4966 "Referring to an argument in another function!", &I);
4967 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
4968 Check(GV->getParent() == &M, "Referencing global in another module!", &I,
4969 &M, GV, GV->getParent());
4970 } else if (isa<Instruction>(I.getOperand(i))) {
4971 verifyDominatesUse(I, i);
4972 } else if (isa<InlineAsm>(I.getOperand(i))) {
4973 Check(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
4974 "Cannot take the address of an inline asm!", &I);
4975 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
4976 if (CE->getType()->isPtrOrPtrVectorTy()) {
4977 // If we have a ConstantExpr pointer, we need to see if it came from an
4978 // illegal bitcast.
4979 visitConstantExprsRecursively(CE);
4980 }
4981 }
4982 }
4983
4984 if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
4985 Check(I.getType()->isFPOrFPVectorTy(),
4986 "fpmath requires a floating point result!", &I);
4987 Check(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
4988 if (ConstantFP *CFP0 =
4989 mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
4990 const APFloat &Accuracy = CFP0->getValueAPF();
4991 Check(&Accuracy.getSemantics() == &APFloat::IEEEsingle(),
4992 "fpmath accuracy must have float type", &I);
4993 Check(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
4994 "fpmath accuracy not a positive number!", &I);
4995 } else {
4996 Check(false, "invalid fpmath accuracy!", &I);
4997 }
4998 }
4999
5000 if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
5001 Check(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
5002 "Ranges are only for loads, calls and invokes!", &I);
5003 visitRangeMetadata(I, Range, I.getType());
5004 }
5005
5006 if (I.hasMetadata(LLVMContext::MD_invariant_group)) {
5007 Check(isa<LoadInst>(I) || isa<StoreInst>(I),
5008 "invariant.group metadata is only for loads and stores", &I);
5009 }
5010
5011 if (MDNode *MD = I.getMetadata(LLVMContext::MD_nonnull)) {
5012 Check(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
5013 &I);
5014 Check(isa<LoadInst>(I),
5015 "nonnull applies only to load instructions, use attributes"
5016 " for calls or invokes",
5017 &I);
5018 Check(MD->getNumOperands() == 0, "nonnull metadata must be empty", &I);
5019 }
5020
5021 if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
5022 visitDereferenceableMetadata(I, MD);
5023
5024 if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
5025 visitDereferenceableMetadata(I, MD);
5026
5027 if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa))
5028 TBAAVerifyHelper.visitTBAAMetadata(I, TBAA);
5029
5030 if (MDNode *MD = I.getMetadata(LLVMContext::MD_noalias))
5031 visitAliasScopeListMetadata(MD);
5032 if (MDNode *MD = I.getMetadata(LLVMContext::MD_alias_scope))
5033 visitAliasScopeListMetadata(MD);
5034
5035 if (MDNode *MD = I.getMetadata(LLVMContext::MD_access_group))
5036 visitAccessGroupMetadata(MD);
5037
5038 if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
5039 Check(I.getType()->isPointerTy(), "align applies only to pointer types",
5040 &I);
5041 Check(isa<LoadInst>(I),
5042 "align applies only to load instructions, "
5043 "use attributes for calls or invokes",
5044 &I);
5045 Check(AlignMD->getNumOperands() == 1, "align takes one operand!", &I);
5046 ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0));
5047 Check(CI && CI->getType()->isIntegerTy(64),
5048 "align metadata value must be an i64!", &I);
5049 uint64_t Align = CI->getZExtValue();
5050 Check(isPowerOf2_64(Align), "align metadata value must be a power of 2!",
5051 &I);
5052 Check(Align <= Value::MaximumAlignment,
5053 "alignment is larger that implementation defined limit", &I);
5054 }
5055
5056 if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof))
5057 visitProfMetadata(I, MD);
5058
5059 if (MDNode *MD = I.getMetadata(LLVMContext::MD_memprof))
5060 visitMemProfMetadata(I, MD);
5061
5062 if (MDNode *MD = I.getMetadata(LLVMContext::MD_callsite))
5063 visitCallsiteMetadata(I, MD);
5064
5065 if (MDNode *MD = I.getMetadata(LLVMContext::MD_DIAssignID))
5066 visitDIAssignIDMetadata(I, MD);
5067
5068 if (MDNode *Annotation = I.getMetadata(LLVMContext::MD_annotation))
5069 visitAnnotationMetadata(Annotation);
5070
5071 if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
5072 CheckDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
5073 visitMDNode(*N, AreDebugLocsAllowed::Yes);
5074 }
5075
5076 if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
5077 verifyFragmentExpression(*DII);
5078 verifyNotEntryValue(*DII);
5079 }
5080
5081 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
5082 I.getAllMetadata(MDs);
5083 for (auto Attachment : MDs) {
5084 unsigned Kind = Attachment.first;
5085 auto AllowLocs =
5086 (Kind == LLVMContext::MD_dbg || Kind == LLVMContext::MD_loop)
5087 ? AreDebugLocsAllowed::Yes
5088 : AreDebugLocsAllowed::No;
5089 visitMDNode(*Attachment.second, AllowLocs);
5090 }
5091
5092 InstsInThisBlock.insert(&I);
5093 }
5094
5095 /// Allow intrinsics to be verified in different ways.
visitIntrinsicCall(Intrinsic::ID ID,CallBase & Call)5096 void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
5097 Function *IF = Call.getCalledFunction();
5098 Check(IF->isDeclaration(), "Intrinsic functions should never be defined!",
5099 IF);
5100
5101 // Verify that the intrinsic prototype lines up with what the .td files
5102 // describe.
5103 FunctionType *IFTy = IF->getFunctionType();
5104 bool IsVarArg = IFTy->isVarArg();
5105
5106 SmallVector<Intrinsic::IITDescriptor, 8> Table;
5107 getIntrinsicInfoTableEntries(ID, Table);
5108 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
5109
5110 // Walk the descriptors to extract overloaded types.
5111 SmallVector<Type *, 4> ArgTys;
5112 Intrinsic::MatchIntrinsicTypesResult Res =
5113 Intrinsic::matchIntrinsicSignature(IFTy, TableRef, ArgTys);
5114 Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchRet,
5115 "Intrinsic has incorrect return type!", IF);
5116 Check(Res != Intrinsic::MatchIntrinsicTypes_NoMatchArg,
5117 "Intrinsic has incorrect argument type!", IF);
5118
5119 // Verify if the intrinsic call matches the vararg property.
5120 if (IsVarArg)
5121 Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
5122 "Intrinsic was not defined with variable arguments!", IF);
5123 else
5124 Check(!Intrinsic::matchIntrinsicVarArg(IsVarArg, TableRef),
5125 "Callsite was not defined with variable arguments!", IF);
5126
5127 // All descriptors should be absorbed by now.
5128 Check(TableRef.empty(), "Intrinsic has too few arguments!", IF);
5129
5130 // Now that we have the intrinsic ID and the actual argument types (and we
5131 // know they are legal for the intrinsic!) get the intrinsic name through the
5132 // usual means. This allows us to verify the mangling of argument types into
5133 // the name.
5134 const std::string ExpectedName =
5135 Intrinsic::getName(ID, ArgTys, IF->getParent(), IFTy);
5136 Check(ExpectedName == IF->getName(),
5137 "Intrinsic name not mangled correctly for type arguments! "
5138 "Should be: " +
5139 ExpectedName,
5140 IF);
5141
5142 // If the intrinsic takes MDNode arguments, verify that they are either global
5143 // or are local to *this* function.
5144 for (Value *V : Call.args()) {
5145 if (auto *MD = dyn_cast<MetadataAsValue>(V))
5146 visitMetadataAsValue(*MD, Call.getCaller());
5147 if (auto *Const = dyn_cast<Constant>(V))
5148 Check(!Const->getType()->isX86_AMXTy(),
5149 "const x86_amx is not allowed in argument!");
5150 }
5151
5152 switch (ID) {
5153 default:
5154 break;
5155 case Intrinsic::assume: {
5156 for (auto &Elem : Call.bundle_op_infos()) {
5157 unsigned ArgCount = Elem.End - Elem.Begin;
5158 // Separate storage assumptions are special insofar as they're the only
5159 // operand bundles allowed on assumes that aren't parameter attributes.
5160 if (Elem.Tag->getKey() == "separate_storage") {
5161 Check(ArgCount == 2,
5162 "separate_storage assumptions should have 2 arguments", Call);
5163 Check(Call.getOperand(Elem.Begin)->getType()->isPointerTy() &&
5164 Call.getOperand(Elem.Begin + 1)->getType()->isPointerTy(),
5165 "arguments to separate_storage assumptions should be pointers",
5166 Call);
5167 return;
5168 }
5169 Check(Elem.Tag->getKey() == "ignore" ||
5170 Attribute::isExistingAttribute(Elem.Tag->getKey()),
5171 "tags must be valid attribute names", Call);
5172 Attribute::AttrKind Kind =
5173 Attribute::getAttrKindFromName(Elem.Tag->getKey());
5174 if (Kind == Attribute::Alignment) {
5175 Check(ArgCount <= 3 && ArgCount >= 2,
5176 "alignment assumptions should have 2 or 3 arguments", Call);
5177 Check(Call.getOperand(Elem.Begin)->getType()->isPointerTy(),
5178 "first argument should be a pointer", Call);
5179 Check(Call.getOperand(Elem.Begin + 1)->getType()->isIntegerTy(),
5180 "second argument should be an integer", Call);
5181 if (ArgCount == 3)
5182 Check(Call.getOperand(Elem.Begin + 2)->getType()->isIntegerTy(),
5183 "third argument should be an integer if present", Call);
5184 return;
5185 }
5186 Check(ArgCount <= 2, "too many arguments", Call);
5187 if (Kind == Attribute::None)
5188 break;
5189 if (Attribute::isIntAttrKind(Kind)) {
5190 Check(ArgCount == 2, "this attribute should have 2 arguments", Call);
5191 Check(isa<ConstantInt>(Call.getOperand(Elem.Begin + 1)),
5192 "the second argument should be a constant integral value", Call);
5193 } else if (Attribute::canUseAsParamAttr(Kind)) {
5194 Check((ArgCount) == 1, "this attribute should have one argument", Call);
5195 } else if (Attribute::canUseAsFnAttr(Kind)) {
5196 Check((ArgCount) == 0, "this attribute has no argument", Call);
5197 }
5198 }
5199 break;
5200 }
5201 case Intrinsic::coro_id: {
5202 auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
5203 if (isa<ConstantPointerNull>(InfoArg))
5204 break;
5205 auto *GV = dyn_cast<GlobalVariable>(InfoArg);
5206 Check(GV && GV->isConstant() && GV->hasDefinitiveInitializer(),
5207 "info argument of llvm.coro.id must refer to an initialized "
5208 "constant");
5209 Constant *Init = GV->getInitializer();
5210 Check(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init),
5211 "info argument of llvm.coro.id must refer to either a struct or "
5212 "an array");
5213 break;
5214 }
5215 case Intrinsic::is_fpclass: {
5216 const ConstantInt *TestMask = cast<ConstantInt>(Call.getOperand(1));
5217 Check((TestMask->getZExtValue() & ~static_cast<unsigned>(fcAllFlags)) == 0,
5218 "unsupported bits for llvm.is.fpclass test mask");
5219 break;
5220 }
5221 case Intrinsic::fptrunc_round: {
5222 // Check the rounding mode
5223 Metadata *MD = nullptr;
5224 auto *MAV = dyn_cast<MetadataAsValue>(Call.getOperand(1));
5225 if (MAV)
5226 MD = MAV->getMetadata();
5227
5228 Check(MD != nullptr, "missing rounding mode argument", Call);
5229
5230 Check(isa<MDString>(MD),
5231 ("invalid value for llvm.fptrunc.round metadata operand"
5232 " (the operand should be a string)"),
5233 MD);
5234
5235 std::optional<RoundingMode> RoundMode =
5236 convertStrToRoundingMode(cast<MDString>(MD)->getString());
5237 Check(RoundMode && *RoundMode != RoundingMode::Dynamic,
5238 "unsupported rounding mode argument", Call);
5239 break;
5240 }
5241 #define BEGIN_REGISTER_VP_INTRINSIC(VPID, ...) case Intrinsic::VPID:
5242 #include "llvm/IR/VPIntrinsics.def"
5243 visitVPIntrinsic(cast<VPIntrinsic>(Call));
5244 break;
5245 #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC) \
5246 case Intrinsic::INTRINSIC:
5247 #include "llvm/IR/ConstrainedOps.def"
5248 visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call));
5249 break;
5250 case Intrinsic::dbg_declare: // llvm.dbg.declare
5251 Check(isa<MetadataAsValue>(Call.getArgOperand(0)),
5252 "invalid llvm.dbg.declare intrinsic call 1", Call);
5253 visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call));
5254 break;
5255 case Intrinsic::dbg_value: // llvm.dbg.value
5256 visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call));
5257 break;
5258 case Intrinsic::dbg_assign: // llvm.dbg.assign
5259 visitDbgIntrinsic("assign", cast<DbgVariableIntrinsic>(Call));
5260 break;
5261 case Intrinsic::dbg_label: // llvm.dbg.label
5262 visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call));
5263 break;
5264 case Intrinsic::memcpy:
5265 case Intrinsic::memcpy_inline:
5266 case Intrinsic::memmove:
5267 case Intrinsic::memset:
5268 case Intrinsic::memset_inline: {
5269 break;
5270 }
5271 case Intrinsic::memcpy_element_unordered_atomic:
5272 case Intrinsic::memmove_element_unordered_atomic:
5273 case Intrinsic::memset_element_unordered_atomic: {
5274 const auto *AMI = cast<AtomicMemIntrinsic>(&Call);
5275
5276 ConstantInt *ElementSizeCI =
5277 cast<ConstantInt>(AMI->getRawElementSizeInBytes());
5278 const APInt &ElementSizeVal = ElementSizeCI->getValue();
5279 Check(ElementSizeVal.isPowerOf2(),
5280 "element size of the element-wise atomic memory intrinsic "
5281 "must be a power of 2",
5282 Call);
5283
5284 auto IsValidAlignment = [&](MaybeAlign Alignment) {
5285 return Alignment && ElementSizeVal.ule(Alignment->value());
5286 };
5287 Check(IsValidAlignment(AMI->getDestAlign()),
5288 "incorrect alignment of the destination argument", Call);
5289 if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) {
5290 Check(IsValidAlignment(AMT->getSourceAlign()),
5291 "incorrect alignment of the source argument", Call);
5292 }
5293 break;
5294 }
5295 case Intrinsic::call_preallocated_setup: {
5296 auto *NumArgs = dyn_cast<ConstantInt>(Call.getArgOperand(0));
5297 Check(NumArgs != nullptr,
5298 "llvm.call.preallocated.setup argument must be a constant");
5299 bool FoundCall = false;
5300 for (User *U : Call.users()) {
5301 auto *UseCall = dyn_cast<CallBase>(U);
5302 Check(UseCall != nullptr,
5303 "Uses of llvm.call.preallocated.setup must be calls");
5304 const Function *Fn = UseCall->getCalledFunction();
5305 if (Fn && Fn->getIntrinsicID() == Intrinsic::call_preallocated_arg) {
5306 auto *AllocArgIndex = dyn_cast<ConstantInt>(UseCall->getArgOperand(1));
5307 Check(AllocArgIndex != nullptr,
5308 "llvm.call.preallocated.alloc arg index must be a constant");
5309 auto AllocArgIndexInt = AllocArgIndex->getValue();
5310 Check(AllocArgIndexInt.sge(0) &&
5311 AllocArgIndexInt.slt(NumArgs->getValue()),
5312 "llvm.call.preallocated.alloc arg index must be between 0 and "
5313 "corresponding "
5314 "llvm.call.preallocated.setup's argument count");
5315 } else if (Fn && Fn->getIntrinsicID() ==
5316 Intrinsic::call_preallocated_teardown) {
5317 // nothing to do
5318 } else {
5319 Check(!FoundCall, "Can have at most one call corresponding to a "
5320 "llvm.call.preallocated.setup");
5321 FoundCall = true;
5322 size_t NumPreallocatedArgs = 0;
5323 for (unsigned i = 0; i < UseCall->arg_size(); i++) {
5324 if (UseCall->paramHasAttr(i, Attribute::Preallocated)) {
5325 ++NumPreallocatedArgs;
5326 }
5327 }
5328 Check(NumPreallocatedArgs != 0,
5329 "cannot use preallocated intrinsics on a call without "
5330 "preallocated arguments");
5331 Check(NumArgs->equalsInt(NumPreallocatedArgs),
5332 "llvm.call.preallocated.setup arg size must be equal to number "
5333 "of preallocated arguments "
5334 "at call site",
5335 Call, *UseCall);
5336 // getOperandBundle() cannot be called if more than one of the operand
5337 // bundle exists. There is already a check elsewhere for this, so skip
5338 // here if we see more than one.
5339 if (UseCall->countOperandBundlesOfType(LLVMContext::OB_preallocated) >
5340 1) {
5341 return;
5342 }
5343 auto PreallocatedBundle =
5344 UseCall->getOperandBundle(LLVMContext::OB_preallocated);
5345 Check(PreallocatedBundle,
5346 "Use of llvm.call.preallocated.setup outside intrinsics "
5347 "must be in \"preallocated\" operand bundle");
5348 Check(PreallocatedBundle->Inputs.front().get() == &Call,
5349 "preallocated bundle must have token from corresponding "
5350 "llvm.call.preallocated.setup");
5351 }
5352 }
5353 break;
5354 }
5355 case Intrinsic::call_preallocated_arg: {
5356 auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
5357 Check(Token && Token->getCalledFunction()->getIntrinsicID() ==
5358 Intrinsic::call_preallocated_setup,
5359 "llvm.call.preallocated.arg token argument must be a "
5360 "llvm.call.preallocated.setup");
5361 Check(Call.hasFnAttr(Attribute::Preallocated),
5362 "llvm.call.preallocated.arg must be called with a \"preallocated\" "
5363 "call site attribute");
5364 break;
5365 }
5366 case Intrinsic::call_preallocated_teardown: {
5367 auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
5368 Check(Token && Token->getCalledFunction()->getIntrinsicID() ==
5369 Intrinsic::call_preallocated_setup,
5370 "llvm.call.preallocated.teardown token argument must be a "
5371 "llvm.call.preallocated.setup");
5372 break;
5373 }
5374 case Intrinsic::gcroot:
5375 case Intrinsic::gcwrite:
5376 case Intrinsic::gcread:
5377 if (ID == Intrinsic::gcroot) {
5378 AllocaInst *AI =
5379 dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts());
5380 Check(AI, "llvm.gcroot parameter #1 must be an alloca.", Call);
5381 Check(isa<Constant>(Call.getArgOperand(1)),
5382 "llvm.gcroot parameter #2 must be a constant.", Call);
5383 if (!AI->getAllocatedType()->isPointerTy()) {
5384 Check(!isa<ConstantPointerNull>(Call.getArgOperand(1)),
5385 "llvm.gcroot parameter #1 must either be a pointer alloca, "
5386 "or argument #2 must be a non-null constant.",
5387 Call);
5388 }
5389 }
5390
5391 Check(Call.getParent()->getParent()->hasGC(),
5392 "Enclosing function does not use GC.", Call);
5393 break;
5394 case Intrinsic::init_trampoline:
5395 Check(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()),
5396 "llvm.init_trampoline parameter #2 must resolve to a function.",
5397 Call);
5398 break;
5399 case Intrinsic::prefetch:
5400 Check(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2,
5401 "rw argument to llvm.prefetch must be 0-1", Call);
5402 Check(cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
5403 "locality argument to llvm.prefetch must be 0-3", Call);
5404 Check(cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue() < 2,
5405 "cache type argument to llvm.prefetch must be 0-1", Call);
5406 break;
5407 case Intrinsic::stackprotector:
5408 Check(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()),
5409 "llvm.stackprotector parameter #2 must resolve to an alloca.", Call);
5410 break;
5411 case Intrinsic::localescape: {
5412 BasicBlock *BB = Call.getParent();
5413 Check(BB->isEntryBlock(), "llvm.localescape used outside of entry block",
5414 Call);
5415 Check(!SawFrameEscape, "multiple calls to llvm.localescape in one function",
5416 Call);
5417 for (Value *Arg : Call.args()) {
5418 if (isa<ConstantPointerNull>(Arg))
5419 continue; // Null values are allowed as placeholders.
5420 auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
5421 Check(AI && AI->isStaticAlloca(),
5422 "llvm.localescape only accepts static allocas", Call);
5423 }
5424 FrameEscapeInfo[BB->getParent()].first = Call.arg_size();
5425 SawFrameEscape = true;
5426 break;
5427 }
5428 case Intrinsic::localrecover: {
5429 Value *FnArg = Call.getArgOperand(0)->stripPointerCasts();
5430 Function *Fn = dyn_cast<Function>(FnArg);
5431 Check(Fn && !Fn->isDeclaration(),
5432 "llvm.localrecover first "
5433 "argument must be function defined in this module",
5434 Call);
5435 auto *IdxArg = cast<ConstantInt>(Call.getArgOperand(2));
5436 auto &Entry = FrameEscapeInfo[Fn];
5437 Entry.second = unsigned(
5438 std::max(uint64_t(Entry.second), IdxArg->getLimitedValue(~0U) + 1));
5439 break;
5440 }
5441
5442 case Intrinsic::experimental_gc_statepoint:
5443 if (auto *CI = dyn_cast<CallInst>(&Call))
5444 Check(!CI->isInlineAsm(),
5445 "gc.statepoint support for inline assembly unimplemented", CI);
5446 Check(Call.getParent()->getParent()->hasGC(),
5447 "Enclosing function does not use GC.", Call);
5448
5449 verifyStatepoint(Call);
5450 break;
5451 case Intrinsic::experimental_gc_result: {
5452 Check(Call.getParent()->getParent()->hasGC(),
5453 "Enclosing function does not use GC.", Call);
5454
5455 auto *Statepoint = Call.getArgOperand(0);
5456 if (isa<UndefValue>(Statepoint))
5457 break;
5458
5459 // Are we tied to a statepoint properly?
5460 const auto *StatepointCall = dyn_cast<CallBase>(Statepoint);
5461 const Function *StatepointFn =
5462 StatepointCall ? StatepointCall->getCalledFunction() : nullptr;
5463 Check(StatepointFn && StatepointFn->isDeclaration() &&
5464 StatepointFn->getIntrinsicID() ==
5465 Intrinsic::experimental_gc_statepoint,
5466 "gc.result operand #1 must be from a statepoint", Call,
5467 Call.getArgOperand(0));
5468
5469 // Check that result type matches wrapped callee.
5470 auto *TargetFuncType =
5471 cast<FunctionType>(StatepointCall->getParamElementType(2));
5472 Check(Call.getType() == TargetFuncType->getReturnType(),
5473 "gc.result result type does not match wrapped callee", Call);
5474 break;
5475 }
5476 case Intrinsic::experimental_gc_relocate: {
5477 Check(Call.arg_size() == 3, "wrong number of arguments", Call);
5478
5479 Check(isa<PointerType>(Call.getType()->getScalarType()),
5480 "gc.relocate must return a pointer or a vector of pointers", Call);
5481
5482 // Check that this relocate is correctly tied to the statepoint
5483
5484 // This is case for relocate on the unwinding path of an invoke statepoint
5485 if (LandingPadInst *LandingPad =
5486 dyn_cast<LandingPadInst>(Call.getArgOperand(0))) {
5487
5488 const BasicBlock *InvokeBB =
5489 LandingPad->getParent()->getUniquePredecessor();
5490
5491 // Landingpad relocates should have only one predecessor with invoke
5492 // statepoint terminator
5493 Check(InvokeBB, "safepoints should have unique landingpads",
5494 LandingPad->getParent());
5495 Check(InvokeBB->getTerminator(), "safepoint block should be well formed",
5496 InvokeBB);
5497 Check(isa<GCStatepointInst>(InvokeBB->getTerminator()),
5498 "gc relocate should be linked to a statepoint", InvokeBB);
5499 } else {
5500 // In all other cases relocate should be tied to the statepoint directly.
5501 // This covers relocates on a normal return path of invoke statepoint and
5502 // relocates of a call statepoint.
5503 auto *Token = Call.getArgOperand(0);
5504 Check(isa<GCStatepointInst>(Token) || isa<UndefValue>(Token),
5505 "gc relocate is incorrectly tied to the statepoint", Call, Token);
5506 }
5507
5508 // Verify rest of the relocate arguments.
5509 const Value &StatepointCall = *cast<GCRelocateInst>(Call).getStatepoint();
5510
5511 // Both the base and derived must be piped through the safepoint.
5512 Value *Base = Call.getArgOperand(1);
5513 Check(isa<ConstantInt>(Base),
5514 "gc.relocate operand #2 must be integer offset", Call);
5515
5516 Value *Derived = Call.getArgOperand(2);
5517 Check(isa<ConstantInt>(Derived),
5518 "gc.relocate operand #3 must be integer offset", Call);
5519
5520 const uint64_t BaseIndex = cast<ConstantInt>(Base)->getZExtValue();
5521 const uint64_t DerivedIndex = cast<ConstantInt>(Derived)->getZExtValue();
5522
5523 // Check the bounds
5524 if (isa<UndefValue>(StatepointCall))
5525 break;
5526 if (auto Opt = cast<GCStatepointInst>(StatepointCall)
5527 .getOperandBundle(LLVMContext::OB_gc_live)) {
5528 Check(BaseIndex < Opt->Inputs.size(),
5529 "gc.relocate: statepoint base index out of bounds", Call);
5530 Check(DerivedIndex < Opt->Inputs.size(),
5531 "gc.relocate: statepoint derived index out of bounds", Call);
5532 }
5533
5534 // Relocated value must be either a pointer type or vector-of-pointer type,
5535 // but gc_relocate does not need to return the same pointer type as the
5536 // relocated pointer. It can be casted to the correct type later if it's
5537 // desired. However, they must have the same address space and 'vectorness'
5538 GCRelocateInst &Relocate = cast<GCRelocateInst>(Call);
5539 auto *ResultType = Call.getType();
5540 auto *DerivedType = Relocate.getDerivedPtr()->getType();
5541 auto *BaseType = Relocate.getBasePtr()->getType();
5542
5543 Check(BaseType->isPtrOrPtrVectorTy(),
5544 "gc.relocate: relocated value must be a pointer", Call);
5545 Check(DerivedType->isPtrOrPtrVectorTy(),
5546 "gc.relocate: relocated value must be a pointer", Call);
5547
5548 Check(ResultType->isVectorTy() == DerivedType->isVectorTy(),
5549 "gc.relocate: vector relocates to vector and pointer to pointer",
5550 Call);
5551 Check(
5552 ResultType->getPointerAddressSpace() ==
5553 DerivedType->getPointerAddressSpace(),
5554 "gc.relocate: relocating a pointer shouldn't change its address space",
5555 Call);
5556
5557 auto GC = llvm::getGCStrategy(Relocate.getFunction()->getGC());
5558 Check(GC, "gc.relocate: calling function must have GCStrategy",
5559 Call.getFunction());
5560 if (GC) {
5561 auto isGCPtr = [&GC](Type *PTy) {
5562 return GC->isGCManagedPointer(PTy->getScalarType()).value_or(true);
5563 };
5564 Check(isGCPtr(ResultType), "gc.relocate: must return gc pointer", Call);
5565 Check(isGCPtr(BaseType),
5566 "gc.relocate: relocated value must be a gc pointer", Call);
5567 Check(isGCPtr(DerivedType),
5568 "gc.relocate: relocated value must be a gc pointer", Call);
5569 }
5570 break;
5571 }
5572 case Intrinsic::eh_exceptioncode:
5573 case Intrinsic::eh_exceptionpointer: {
5574 Check(isa<CatchPadInst>(Call.getArgOperand(0)),
5575 "eh.exceptionpointer argument must be a catchpad", Call);
5576 break;
5577 }
5578 case Intrinsic::get_active_lane_mask: {
5579 Check(Call.getType()->isVectorTy(),
5580 "get_active_lane_mask: must return a "
5581 "vector",
5582 Call);
5583 auto *ElemTy = Call.getType()->getScalarType();
5584 Check(ElemTy->isIntegerTy(1),
5585 "get_active_lane_mask: element type is not "
5586 "i1",
5587 Call);
5588 break;
5589 }
5590 case Intrinsic::experimental_get_vector_length: {
5591 ConstantInt *VF = cast<ConstantInt>(Call.getArgOperand(1));
5592 Check(!VF->isNegative() && !VF->isZero(),
5593 "get_vector_length: VF must be positive", Call);
5594 break;
5595 }
5596 case Intrinsic::masked_load: {
5597 Check(Call.getType()->isVectorTy(), "masked_load: must return a vector",
5598 Call);
5599
5600 ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(1));
5601 Value *Mask = Call.getArgOperand(2);
5602 Value *PassThru = Call.getArgOperand(3);
5603 Check(Mask->getType()->isVectorTy(), "masked_load: mask must be vector",
5604 Call);
5605 Check(Alignment->getValue().isPowerOf2(),
5606 "masked_load: alignment must be a power of 2", Call);
5607 Check(PassThru->getType() == Call.getType(),
5608 "masked_load: pass through and return type must match", Call);
5609 Check(cast<VectorType>(Mask->getType())->getElementCount() ==
5610 cast<VectorType>(Call.getType())->getElementCount(),
5611 "masked_load: vector mask must be same length as return", Call);
5612 break;
5613 }
5614 case Intrinsic::masked_store: {
5615 Value *Val = Call.getArgOperand(0);
5616 ConstantInt *Alignment = cast<ConstantInt>(Call.getArgOperand(2));
5617 Value *Mask = Call.getArgOperand(3);
5618 Check(Mask->getType()->isVectorTy(), "masked_store: mask must be vector",
5619 Call);
5620 Check(Alignment->getValue().isPowerOf2(),
5621 "masked_store: alignment must be a power of 2", Call);
5622 Check(cast<VectorType>(Mask->getType())->getElementCount() ==
5623 cast<VectorType>(Val->getType())->getElementCount(),
5624 "masked_store: vector mask must be same length as value", Call);
5625 break;
5626 }
5627
5628 case Intrinsic::masked_gather: {
5629 const APInt &Alignment =
5630 cast<ConstantInt>(Call.getArgOperand(1))->getValue();
5631 Check(Alignment.isZero() || Alignment.isPowerOf2(),
5632 "masked_gather: alignment must be 0 or a power of 2", Call);
5633 break;
5634 }
5635 case Intrinsic::masked_scatter: {
5636 const APInt &Alignment =
5637 cast<ConstantInt>(Call.getArgOperand(2))->getValue();
5638 Check(Alignment.isZero() || Alignment.isPowerOf2(),
5639 "masked_scatter: alignment must be 0 or a power of 2", Call);
5640 break;
5641 }
5642
5643 case Intrinsic::experimental_guard: {
5644 Check(isa<CallInst>(Call), "experimental_guard cannot be invoked", Call);
5645 Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
5646 "experimental_guard must have exactly one "
5647 "\"deopt\" operand bundle");
5648 break;
5649 }
5650
5651 case Intrinsic::experimental_deoptimize: {
5652 Check(isa<CallInst>(Call), "experimental_deoptimize cannot be invoked",
5653 Call);
5654 Check(Call.countOperandBundlesOfType(LLVMContext::OB_deopt) == 1,
5655 "experimental_deoptimize must have exactly one "
5656 "\"deopt\" operand bundle");
5657 Check(Call.getType() == Call.getFunction()->getReturnType(),
5658 "experimental_deoptimize return type must match caller return type");
5659
5660 if (isa<CallInst>(Call)) {
5661 auto *RI = dyn_cast<ReturnInst>(Call.getNextNode());
5662 Check(RI,
5663 "calls to experimental_deoptimize must be followed by a return");
5664
5665 if (!Call.getType()->isVoidTy() && RI)
5666 Check(RI->getReturnValue() == &Call,
5667 "calls to experimental_deoptimize must be followed by a return "
5668 "of the value computed by experimental_deoptimize");
5669 }
5670
5671 break;
5672 }
5673 case Intrinsic::vector_reduce_and:
5674 case Intrinsic::vector_reduce_or:
5675 case Intrinsic::vector_reduce_xor:
5676 case Intrinsic::vector_reduce_add:
5677 case Intrinsic::vector_reduce_mul:
5678 case Intrinsic::vector_reduce_smax:
5679 case Intrinsic::vector_reduce_smin:
5680 case Intrinsic::vector_reduce_umax:
5681 case Intrinsic::vector_reduce_umin: {
5682 Type *ArgTy = Call.getArgOperand(0)->getType();
5683 Check(ArgTy->isIntOrIntVectorTy() && ArgTy->isVectorTy(),
5684 "Intrinsic has incorrect argument type!");
5685 break;
5686 }
5687 case Intrinsic::vector_reduce_fmax:
5688 case Intrinsic::vector_reduce_fmin: {
5689 Type *ArgTy = Call.getArgOperand(0)->getType();
5690 Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5691 "Intrinsic has incorrect argument type!");
5692 break;
5693 }
5694 case Intrinsic::vector_reduce_fadd:
5695 case Intrinsic::vector_reduce_fmul: {
5696 // Unlike the other reductions, the first argument is a start value. The
5697 // second argument is the vector to be reduced.
5698 Type *ArgTy = Call.getArgOperand(1)->getType();
5699 Check(ArgTy->isFPOrFPVectorTy() && ArgTy->isVectorTy(),
5700 "Intrinsic has incorrect argument type!");
5701 break;
5702 }
5703 case Intrinsic::smul_fix:
5704 case Intrinsic::smul_fix_sat:
5705 case Intrinsic::umul_fix:
5706 case Intrinsic::umul_fix_sat:
5707 case Intrinsic::sdiv_fix:
5708 case Intrinsic::sdiv_fix_sat:
5709 case Intrinsic::udiv_fix:
5710 case Intrinsic::udiv_fix_sat: {
5711 Value *Op1 = Call.getArgOperand(0);
5712 Value *Op2 = Call.getArgOperand(1);
5713 Check(Op1->getType()->isIntOrIntVectorTy(),
5714 "first operand of [us][mul|div]_fix[_sat] must be an int type or "
5715 "vector of ints");
5716 Check(Op2->getType()->isIntOrIntVectorTy(),
5717 "second operand of [us][mul|div]_fix[_sat] must be an int type or "
5718 "vector of ints");
5719
5720 auto *Op3 = cast<ConstantInt>(Call.getArgOperand(2));
5721 Check(Op3->getType()->isIntegerTy(),
5722 "third operand of [us][mul|div]_fix[_sat] must be an int type");
5723 Check(Op3->getBitWidth() <= 32,
5724 "third operand of [us][mul|div]_fix[_sat] must fit within 32 bits");
5725
5726 if (ID == Intrinsic::smul_fix || ID == Intrinsic::smul_fix_sat ||
5727 ID == Intrinsic::sdiv_fix || ID == Intrinsic::sdiv_fix_sat) {
5728 Check(Op3->getZExtValue() < Op1->getType()->getScalarSizeInBits(),
5729 "the scale of s[mul|div]_fix[_sat] must be less than the width of "
5730 "the operands");
5731 } else {
5732 Check(Op3->getZExtValue() <= Op1->getType()->getScalarSizeInBits(),
5733 "the scale of u[mul|div]_fix[_sat] must be less than or equal "
5734 "to the width of the operands");
5735 }
5736 break;
5737 }
5738 case Intrinsic::lrint:
5739 case Intrinsic::llrint: {
5740 Type *ValTy = Call.getArgOperand(0)->getType();
5741 Type *ResultTy = Call.getType();
5742 Check(
5743 ValTy->isFPOrFPVectorTy() && ResultTy->isIntOrIntVectorTy(),
5744 "llvm.lrint, llvm.llrint: argument must be floating-point or vector "
5745 "of floating-points, and result must be integer or vector of integers",
5746 &Call);
5747 Check(ValTy->isVectorTy() == ResultTy->isVectorTy(),
5748 "llvm.lrint, llvm.llrint: argument and result disagree on vector use",
5749 &Call);
5750 if (ValTy->isVectorTy()) {
5751 Check(cast<VectorType>(ValTy)->getElementCount() ==
5752 cast<VectorType>(ResultTy)->getElementCount(),
5753 "llvm.lrint, llvm.llrint: argument must be same length as result",
5754 &Call);
5755 }
5756 break;
5757 }
5758 case Intrinsic::lround:
5759 case Intrinsic::llround: {
5760 Type *ValTy = Call.getArgOperand(0)->getType();
5761 Type *ResultTy = Call.getType();
5762 Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
5763 "Intrinsic does not support vectors", &Call);
5764 break;
5765 }
5766 case Intrinsic::bswap: {
5767 Type *Ty = Call.getType();
5768 unsigned Size = Ty->getScalarSizeInBits();
5769 Check(Size % 16 == 0, "bswap must be an even number of bytes", &Call);
5770 break;
5771 }
5772 case Intrinsic::invariant_start: {
5773 ConstantInt *InvariantSize = dyn_cast<ConstantInt>(Call.getArgOperand(0));
5774 Check(InvariantSize &&
5775 (!InvariantSize->isNegative() || InvariantSize->isMinusOne()),
5776 "invariant_start parameter must be -1, 0 or a positive number",
5777 &Call);
5778 break;
5779 }
5780 case Intrinsic::matrix_multiply:
5781 case Intrinsic::matrix_transpose:
5782 case Intrinsic::matrix_column_major_load:
5783 case Intrinsic::matrix_column_major_store: {
5784 Function *IF = Call.getCalledFunction();
5785 ConstantInt *Stride = nullptr;
5786 ConstantInt *NumRows;
5787 ConstantInt *NumColumns;
5788 VectorType *ResultTy;
5789 Type *Op0ElemTy = nullptr;
5790 Type *Op1ElemTy = nullptr;
5791 switch (ID) {
5792 case Intrinsic::matrix_multiply: {
5793 NumRows = cast<ConstantInt>(Call.getArgOperand(2));
5794 ConstantInt *N = cast<ConstantInt>(Call.getArgOperand(3));
5795 NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
5796 Check(cast<FixedVectorType>(Call.getArgOperand(0)->getType())
5797 ->getNumElements() ==
5798 NumRows->getZExtValue() * N->getZExtValue(),
5799 "First argument of a matrix operation does not match specified "
5800 "shape!");
5801 Check(cast<FixedVectorType>(Call.getArgOperand(1)->getType())
5802 ->getNumElements() ==
5803 N->getZExtValue() * NumColumns->getZExtValue(),
5804 "Second argument of a matrix operation does not match specified "
5805 "shape!");
5806
5807 ResultTy = cast<VectorType>(Call.getType());
5808 Op0ElemTy =
5809 cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5810 Op1ElemTy =
5811 cast<VectorType>(Call.getArgOperand(1)->getType())->getElementType();
5812 break;
5813 }
5814 case Intrinsic::matrix_transpose:
5815 NumRows = cast<ConstantInt>(Call.getArgOperand(1));
5816 NumColumns = cast<ConstantInt>(Call.getArgOperand(2));
5817 ResultTy = cast<VectorType>(Call.getType());
5818 Op0ElemTy =
5819 cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5820 break;
5821 case Intrinsic::matrix_column_major_load: {
5822 Stride = dyn_cast<ConstantInt>(Call.getArgOperand(1));
5823 NumRows = cast<ConstantInt>(Call.getArgOperand(3));
5824 NumColumns = cast<ConstantInt>(Call.getArgOperand(4));
5825 ResultTy = cast<VectorType>(Call.getType());
5826 break;
5827 }
5828 case Intrinsic::matrix_column_major_store: {
5829 Stride = dyn_cast<ConstantInt>(Call.getArgOperand(2));
5830 NumRows = cast<ConstantInt>(Call.getArgOperand(4));
5831 NumColumns = cast<ConstantInt>(Call.getArgOperand(5));
5832 ResultTy = cast<VectorType>(Call.getArgOperand(0)->getType());
5833 Op0ElemTy =
5834 cast<VectorType>(Call.getArgOperand(0)->getType())->getElementType();
5835 break;
5836 }
5837 default:
5838 llvm_unreachable("unexpected intrinsic");
5839 }
5840
5841 Check(ResultTy->getElementType()->isIntegerTy() ||
5842 ResultTy->getElementType()->isFloatingPointTy(),
5843 "Result type must be an integer or floating-point type!", IF);
5844
5845 if (Op0ElemTy)
5846 Check(ResultTy->getElementType() == Op0ElemTy,
5847 "Vector element type mismatch of the result and first operand "
5848 "vector!",
5849 IF);
5850
5851 if (Op1ElemTy)
5852 Check(ResultTy->getElementType() == Op1ElemTy,
5853 "Vector element type mismatch of the result and second operand "
5854 "vector!",
5855 IF);
5856
5857 Check(cast<FixedVectorType>(ResultTy)->getNumElements() ==
5858 NumRows->getZExtValue() * NumColumns->getZExtValue(),
5859 "Result of a matrix operation does not fit in the returned vector!");
5860
5861 if (Stride)
5862 Check(Stride->getZExtValue() >= NumRows->getZExtValue(),
5863 "Stride must be greater or equal than the number of rows!", IF);
5864
5865 break;
5866 }
5867 case Intrinsic::experimental_vector_splice: {
5868 VectorType *VecTy = cast<VectorType>(Call.getType());
5869 int64_t Idx = cast<ConstantInt>(Call.getArgOperand(2))->getSExtValue();
5870 int64_t KnownMinNumElements = VecTy->getElementCount().getKnownMinValue();
5871 if (Call.getParent() && Call.getParent()->getParent()) {
5872 AttributeList Attrs = Call.getParent()->getParent()->getAttributes();
5873 if (Attrs.hasFnAttr(Attribute::VScaleRange))
5874 KnownMinNumElements *= Attrs.getFnAttrs().getVScaleRangeMin();
5875 }
5876 Check((Idx < 0 && std::abs(Idx) <= KnownMinNumElements) ||
5877 (Idx >= 0 && Idx < KnownMinNumElements),
5878 "The splice index exceeds the range [-VL, VL-1] where VL is the "
5879 "known minimum number of elements in the vector. For scalable "
5880 "vectors the minimum number of elements is determined from "
5881 "vscale_range.",
5882 &Call);
5883 break;
5884 }
5885 case Intrinsic::experimental_stepvector: {
5886 VectorType *VecTy = dyn_cast<VectorType>(Call.getType());
5887 Check(VecTy && VecTy->getScalarType()->isIntegerTy() &&
5888 VecTy->getScalarSizeInBits() >= 8,
5889 "experimental_stepvector only supported for vectors of integers "
5890 "with a bitwidth of at least 8.",
5891 &Call);
5892 break;
5893 }
5894 case Intrinsic::vector_insert: {
5895 Value *Vec = Call.getArgOperand(0);
5896 Value *SubVec = Call.getArgOperand(1);
5897 Value *Idx = Call.getArgOperand(2);
5898 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
5899
5900 VectorType *VecTy = cast<VectorType>(Vec->getType());
5901 VectorType *SubVecTy = cast<VectorType>(SubVec->getType());
5902
5903 ElementCount VecEC = VecTy->getElementCount();
5904 ElementCount SubVecEC = SubVecTy->getElementCount();
5905 Check(VecTy->getElementType() == SubVecTy->getElementType(),
5906 "vector_insert parameters must have the same element "
5907 "type.",
5908 &Call);
5909 Check(IdxN % SubVecEC.getKnownMinValue() == 0,
5910 "vector_insert index must be a constant multiple of "
5911 "the subvector's known minimum vector length.");
5912
5913 // If this insertion is not the 'mixed' case where a fixed vector is
5914 // inserted into a scalable vector, ensure that the insertion of the
5915 // subvector does not overrun the parent vector.
5916 if (VecEC.isScalable() == SubVecEC.isScalable()) {
5917 Check(IdxN < VecEC.getKnownMinValue() &&
5918 IdxN + SubVecEC.getKnownMinValue() <= VecEC.getKnownMinValue(),
5919 "subvector operand of vector_insert would overrun the "
5920 "vector being inserted into.");
5921 }
5922 break;
5923 }
5924 case Intrinsic::vector_extract: {
5925 Value *Vec = Call.getArgOperand(0);
5926 Value *Idx = Call.getArgOperand(1);
5927 unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
5928
5929 VectorType *ResultTy = cast<VectorType>(Call.getType());
5930 VectorType *VecTy = cast<VectorType>(Vec->getType());
5931
5932 ElementCount VecEC = VecTy->getElementCount();
5933 ElementCount ResultEC = ResultTy->getElementCount();
5934
5935 Check(ResultTy->getElementType() == VecTy->getElementType(),
5936 "vector_extract result must have the same element "
5937 "type as the input vector.",
5938 &Call);
5939 Check(IdxN % ResultEC.getKnownMinValue() == 0,
5940 "vector_extract index must be a constant multiple of "
5941 "the result type's known minimum vector length.");
5942
5943 // If this extraction is not the 'mixed' case where a fixed vector is
5944 // extracted from a scalable vector, ensure that the extraction does not
5945 // overrun the parent vector.
5946 if (VecEC.isScalable() == ResultEC.isScalable()) {
5947 Check(IdxN < VecEC.getKnownMinValue() &&
5948 IdxN + ResultEC.getKnownMinValue() <= VecEC.getKnownMinValue(),
5949 "vector_extract would overrun.");
5950 }
5951 break;
5952 }
5953 case Intrinsic::experimental_noalias_scope_decl: {
5954 NoAliasScopeDecls.push_back(cast<IntrinsicInst>(&Call));
5955 break;
5956 }
5957 case Intrinsic::preserve_array_access_index:
5958 case Intrinsic::preserve_struct_access_index:
5959 case Intrinsic::aarch64_ldaxr:
5960 case Intrinsic::aarch64_ldxr:
5961 case Intrinsic::arm_ldaex:
5962 case Intrinsic::arm_ldrex: {
5963 Type *ElemTy = Call.getParamElementType(0);
5964 Check(ElemTy, "Intrinsic requires elementtype attribute on first argument.",
5965 &Call);
5966 break;
5967 }
5968 case Intrinsic::aarch64_stlxr:
5969 case Intrinsic::aarch64_stxr:
5970 case Intrinsic::arm_stlex:
5971 case Intrinsic::arm_strex: {
5972 Type *ElemTy = Call.getAttributes().getParamElementType(1);
5973 Check(ElemTy,
5974 "Intrinsic requires elementtype attribute on second argument.",
5975 &Call);
5976 break;
5977 }
5978 case Intrinsic::aarch64_prefetch: {
5979 Check(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2,
5980 "write argument to llvm.aarch64.prefetch must be 0 or 1", Call);
5981 Check(cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
5982 "target argument to llvm.aarch64.prefetch must be 0-3", Call);
5983 Check(cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue() < 2,
5984 "stream argument to llvm.aarch64.prefetch must be 0 or 1", Call);
5985 Check(cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue() < 2,
5986 "isdata argument to llvm.aarch64.prefetch must be 0 or 1", Call);
5987 break;
5988 }
5989 case Intrinsic::callbr_landingpad: {
5990 const auto *CBR = dyn_cast<CallBrInst>(Call.getOperand(0));
5991 Check(CBR, "intrinstic requires callbr operand", &Call);
5992 if (!CBR)
5993 break;
5994
5995 const BasicBlock *LandingPadBB = Call.getParent();
5996 const BasicBlock *PredBB = LandingPadBB->getUniquePredecessor();
5997 if (!PredBB) {
5998 CheckFailed("Intrinsic in block must have 1 unique predecessor", &Call);
5999 break;
6000 }
6001 if (!isa<CallBrInst>(PredBB->getTerminator())) {
6002 CheckFailed("Intrinsic must have corresponding callbr in predecessor",
6003 &Call);
6004 break;
6005 }
6006 Check(llvm::any_of(CBR->getIndirectDests(),
6007 [LandingPadBB](const BasicBlock *IndDest) {
6008 return IndDest == LandingPadBB;
6009 }),
6010 "Intrinsic's corresponding callbr must have intrinsic's parent basic "
6011 "block in indirect destination list",
6012 &Call);
6013 const Instruction &First = *LandingPadBB->begin();
6014 Check(&First == &Call, "No other instructions may proceed intrinsic",
6015 &Call);
6016 break;
6017 }
6018 case Intrinsic::amdgcn_cs_chain: {
6019 auto CallerCC = Call.getCaller()->getCallingConv();
6020 switch (CallerCC) {
6021 case CallingConv::AMDGPU_CS:
6022 case CallingConv::AMDGPU_CS_Chain:
6023 case CallingConv::AMDGPU_CS_ChainPreserve:
6024 break;
6025 default:
6026 CheckFailed("Intrinsic can only be used from functions with the "
6027 "amdgpu_cs, amdgpu_cs_chain or amdgpu_cs_chain_preserve "
6028 "calling conventions",
6029 &Call);
6030 break;
6031 }
6032
6033 Check(Call.paramHasAttr(2, Attribute::InReg),
6034 "SGPR arguments must have the `inreg` attribute", &Call);
6035 Check(!Call.paramHasAttr(3, Attribute::InReg),
6036 "VGPR arguments must not have the `inreg` attribute", &Call);
6037 break;
6038 }
6039 case Intrinsic::amdgcn_set_inactive_chain_arg: {
6040 auto CallerCC = Call.getCaller()->getCallingConv();
6041 switch (CallerCC) {
6042 case CallingConv::AMDGPU_CS_Chain:
6043 case CallingConv::AMDGPU_CS_ChainPreserve:
6044 break;
6045 default:
6046 CheckFailed("Intrinsic can only be used from functions with the "
6047 "amdgpu_cs_chain or amdgpu_cs_chain_preserve "
6048 "calling conventions",
6049 &Call);
6050 break;
6051 }
6052
6053 unsigned InactiveIdx = 1;
6054 Check(!Call.paramHasAttr(InactiveIdx, Attribute::InReg),
6055 "Value for inactive lanes must not have the `inreg` attribute",
6056 &Call);
6057 Check(isa<Argument>(Call.getArgOperand(InactiveIdx)),
6058 "Value for inactive lanes must be a function argument", &Call);
6059 Check(!cast<Argument>(Call.getArgOperand(InactiveIdx))->hasInRegAttr(),
6060 "Value for inactive lanes must be a VGPR function argument", &Call);
6061 break;
6062 }
6063 case Intrinsic::nvvm_setmaxnreg_inc_sync_aligned_u32:
6064 case Intrinsic::nvvm_setmaxnreg_dec_sync_aligned_u32: {
6065 Value *V = Call.getArgOperand(0);
6066 unsigned RegCount = cast<ConstantInt>(V)->getZExtValue();
6067 Check(RegCount % 8 == 0,
6068 "reg_count argument to nvvm.setmaxnreg must be in multiples of 8");
6069 Check((RegCount >= 24 && RegCount <= 256),
6070 "reg_count argument to nvvm.setmaxnreg must be within [24, 256]");
6071 break;
6072 }
6073 case Intrinsic::experimental_convergence_entry:
6074 LLVM_FALLTHROUGH;
6075 case Intrinsic::experimental_convergence_anchor:
6076 break;
6077 case Intrinsic::experimental_convergence_loop:
6078 break;
6079 case Intrinsic::ptrmask: {
6080 Type *Ty0 = Call.getArgOperand(0)->getType();
6081 Type *Ty1 = Call.getArgOperand(1)->getType();
6082 Check(Ty0->isPtrOrPtrVectorTy(),
6083 "llvm.ptrmask intrinsic first argument must be pointer or vector "
6084 "of pointers",
6085 &Call);
6086 Check(
6087 Ty0->isVectorTy() == Ty1->isVectorTy(),
6088 "llvm.ptrmask intrinsic arguments must be both scalars or both vectors",
6089 &Call);
6090 if (Ty0->isVectorTy())
6091 Check(cast<VectorType>(Ty0)->getElementCount() ==
6092 cast<VectorType>(Ty1)->getElementCount(),
6093 "llvm.ptrmask intrinsic arguments must have the same number of "
6094 "elements",
6095 &Call);
6096 Check(DL.getIndexTypeSizeInBits(Ty0) == Ty1->getScalarSizeInBits(),
6097 "llvm.ptrmask intrinsic second argument bitwidth must match "
6098 "pointer index type size of first argument",
6099 &Call);
6100 break;
6101 }
6102 };
6103
6104 // Verify that there aren't any unmediated control transfers between funclets.
6105 if (IntrinsicInst::mayLowerToFunctionCall(ID)) {
6106 Function *F = Call.getParent()->getParent();
6107 if (F->hasPersonalityFn() &&
6108 isScopedEHPersonality(classifyEHPersonality(F->getPersonalityFn()))) {
6109 // Run EH funclet coloring on-demand and cache results for other intrinsic
6110 // calls in this function
6111 if (BlockEHFuncletColors.empty())
6112 BlockEHFuncletColors = colorEHFunclets(*F);
6113
6114 // Check for catch-/cleanup-pad in first funclet block
6115 bool InEHFunclet = false;
6116 BasicBlock *CallBB = Call.getParent();
6117 const ColorVector &CV = BlockEHFuncletColors.find(CallBB)->second;
6118 assert(CV.size() > 0 && "Uncolored block");
6119 for (BasicBlock *ColorFirstBB : CV)
6120 if (dyn_cast_or_null<FuncletPadInst>(ColorFirstBB->getFirstNonPHI()))
6121 InEHFunclet = true;
6122
6123 // Check for funclet operand bundle
6124 bool HasToken = false;
6125 for (unsigned I = 0, E = Call.getNumOperandBundles(); I != E; ++I)
6126 if (Call.getOperandBundleAt(I).getTagID() == LLVMContext::OB_funclet)
6127 HasToken = true;
6128
6129 // This would cause silent code truncation in WinEHPrepare
6130 if (InEHFunclet)
6131 Check(HasToken, "Missing funclet token on intrinsic call", &Call);
6132 }
6133 }
6134 }
6135
6136 /// Carefully grab the subprogram from a local scope.
6137 ///
6138 /// This carefully grabs the subprogram from a local scope, avoiding the
6139 /// built-in assertions that would typically fire.
getSubprogram(Metadata * LocalScope)6140 static DISubprogram *getSubprogram(Metadata *LocalScope) {
6141 if (!LocalScope)
6142 return nullptr;
6143
6144 if (auto *SP = dyn_cast<DISubprogram>(LocalScope))
6145 return SP;
6146
6147 if (auto *LB = dyn_cast<DILexicalBlockBase>(LocalScope))
6148 return getSubprogram(LB->getRawScope());
6149
6150 // Just return null; broken scope chains are checked elsewhere.
6151 assert(!isa<DILocalScope>(LocalScope) && "Unknown type of local scope");
6152 return nullptr;
6153 }
6154
visitVPIntrinsic(VPIntrinsic & VPI)6155 void Verifier::visitVPIntrinsic(VPIntrinsic &VPI) {
6156 if (auto *VPCast = dyn_cast<VPCastIntrinsic>(&VPI)) {
6157 auto *RetTy = cast<VectorType>(VPCast->getType());
6158 auto *ValTy = cast<VectorType>(VPCast->getOperand(0)->getType());
6159 Check(RetTy->getElementCount() == ValTy->getElementCount(),
6160 "VP cast intrinsic first argument and result vector lengths must be "
6161 "equal",
6162 *VPCast);
6163
6164 switch (VPCast->getIntrinsicID()) {
6165 default:
6166 llvm_unreachable("Unknown VP cast intrinsic");
6167 case Intrinsic::vp_trunc:
6168 Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(),
6169 "llvm.vp.trunc intrinsic first argument and result element type "
6170 "must be integer",
6171 *VPCast);
6172 Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(),
6173 "llvm.vp.trunc intrinsic the bit size of first argument must be "
6174 "larger than the bit size of the return type",
6175 *VPCast);
6176 break;
6177 case Intrinsic::vp_zext:
6178 case Intrinsic::vp_sext:
6179 Check(RetTy->isIntOrIntVectorTy() && ValTy->isIntOrIntVectorTy(),
6180 "llvm.vp.zext or llvm.vp.sext intrinsic first argument and result "
6181 "element type must be integer",
6182 *VPCast);
6183 Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(),
6184 "llvm.vp.zext or llvm.vp.sext intrinsic the bit size of first "
6185 "argument must be smaller than the bit size of the return type",
6186 *VPCast);
6187 break;
6188 case Intrinsic::vp_fptoui:
6189 case Intrinsic::vp_fptosi:
6190 Check(
6191 RetTy->isIntOrIntVectorTy() && ValTy->isFPOrFPVectorTy(),
6192 "llvm.vp.fptoui or llvm.vp.fptosi intrinsic first argument element "
6193 "type must be floating-point and result element type must be integer",
6194 *VPCast);
6195 break;
6196 case Intrinsic::vp_uitofp:
6197 case Intrinsic::vp_sitofp:
6198 Check(
6199 RetTy->isFPOrFPVectorTy() && ValTy->isIntOrIntVectorTy(),
6200 "llvm.vp.uitofp or llvm.vp.sitofp intrinsic first argument element "
6201 "type must be integer and result element type must be floating-point",
6202 *VPCast);
6203 break;
6204 case Intrinsic::vp_fptrunc:
6205 Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(),
6206 "llvm.vp.fptrunc intrinsic first argument and result element type "
6207 "must be floating-point",
6208 *VPCast);
6209 Check(RetTy->getScalarSizeInBits() < ValTy->getScalarSizeInBits(),
6210 "llvm.vp.fptrunc intrinsic the bit size of first argument must be "
6211 "larger than the bit size of the return type",
6212 *VPCast);
6213 break;
6214 case Intrinsic::vp_fpext:
6215 Check(RetTy->isFPOrFPVectorTy() && ValTy->isFPOrFPVectorTy(),
6216 "llvm.vp.fpext intrinsic first argument and result element type "
6217 "must be floating-point",
6218 *VPCast);
6219 Check(RetTy->getScalarSizeInBits() > ValTy->getScalarSizeInBits(),
6220 "llvm.vp.fpext intrinsic the bit size of first argument must be "
6221 "smaller than the bit size of the return type",
6222 *VPCast);
6223 break;
6224 case Intrinsic::vp_ptrtoint:
6225 Check(RetTy->isIntOrIntVectorTy() && ValTy->isPtrOrPtrVectorTy(),
6226 "llvm.vp.ptrtoint intrinsic first argument element type must be "
6227 "pointer and result element type must be integer",
6228 *VPCast);
6229 break;
6230 case Intrinsic::vp_inttoptr:
6231 Check(RetTy->isPtrOrPtrVectorTy() && ValTy->isIntOrIntVectorTy(),
6232 "llvm.vp.inttoptr intrinsic first argument element type must be "
6233 "integer and result element type must be pointer",
6234 *VPCast);
6235 break;
6236 }
6237 }
6238 if (VPI.getIntrinsicID() == Intrinsic::vp_fcmp) {
6239 auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate();
6240 Check(CmpInst::isFPPredicate(Pred),
6241 "invalid predicate for VP FP comparison intrinsic", &VPI);
6242 }
6243 if (VPI.getIntrinsicID() == Intrinsic::vp_icmp) {
6244 auto Pred = cast<VPCmpIntrinsic>(&VPI)->getPredicate();
6245 Check(CmpInst::isIntPredicate(Pred),
6246 "invalid predicate for VP integer comparison intrinsic", &VPI);
6247 }
6248 if (VPI.getIntrinsicID() == Intrinsic::vp_is_fpclass) {
6249 auto TestMask = cast<ConstantInt>(VPI.getOperand(1));
6250 Check((TestMask->getZExtValue() & ~static_cast<unsigned>(fcAllFlags)) == 0,
6251 "unsupported bits for llvm.vp.is.fpclass test mask");
6252 }
6253 }
6254
visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic & FPI)6255 void Verifier::visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI) {
6256 unsigned NumOperands;
6257 bool HasRoundingMD;
6258 switch (FPI.getIntrinsicID()) {
6259 #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
6260 case Intrinsic::INTRINSIC: \
6261 NumOperands = NARG; \
6262 HasRoundingMD = ROUND_MODE; \
6263 break;
6264 #include "llvm/IR/ConstrainedOps.def"
6265 default:
6266 llvm_unreachable("Invalid constrained FP intrinsic!");
6267 }
6268 NumOperands += (1 + HasRoundingMD);
6269 // Compare intrinsics carry an extra predicate metadata operand.
6270 if (isa<ConstrainedFPCmpIntrinsic>(FPI))
6271 NumOperands += 1;
6272 Check((FPI.arg_size() == NumOperands),
6273 "invalid arguments for constrained FP intrinsic", &FPI);
6274
6275 switch (FPI.getIntrinsicID()) {
6276 case Intrinsic::experimental_constrained_lrint:
6277 case Intrinsic::experimental_constrained_llrint: {
6278 Type *ValTy = FPI.getArgOperand(0)->getType();
6279 Type *ResultTy = FPI.getType();
6280 Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
6281 "Intrinsic does not support vectors", &FPI);
6282 }
6283 break;
6284
6285 case Intrinsic::experimental_constrained_lround:
6286 case Intrinsic::experimental_constrained_llround: {
6287 Type *ValTy = FPI.getArgOperand(0)->getType();
6288 Type *ResultTy = FPI.getType();
6289 Check(!ValTy->isVectorTy() && !ResultTy->isVectorTy(),
6290 "Intrinsic does not support vectors", &FPI);
6291 break;
6292 }
6293
6294 case Intrinsic::experimental_constrained_fcmp:
6295 case Intrinsic::experimental_constrained_fcmps: {
6296 auto Pred = cast<ConstrainedFPCmpIntrinsic>(&FPI)->getPredicate();
6297 Check(CmpInst::isFPPredicate(Pred),
6298 "invalid predicate for constrained FP comparison intrinsic", &FPI);
6299 break;
6300 }
6301
6302 case Intrinsic::experimental_constrained_fptosi:
6303 case Intrinsic::experimental_constrained_fptoui: {
6304 Value *Operand = FPI.getArgOperand(0);
6305 ElementCount SrcEC;
6306 Check(Operand->getType()->isFPOrFPVectorTy(),
6307 "Intrinsic first argument must be floating point", &FPI);
6308 if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
6309 SrcEC = cast<VectorType>(OperandT)->getElementCount();
6310 }
6311
6312 Operand = &FPI;
6313 Check(SrcEC.isNonZero() == Operand->getType()->isVectorTy(),
6314 "Intrinsic first argument and result disagree on vector use", &FPI);
6315 Check(Operand->getType()->isIntOrIntVectorTy(),
6316 "Intrinsic result must be an integer", &FPI);
6317 if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
6318 Check(SrcEC == cast<VectorType>(OperandT)->getElementCount(),
6319 "Intrinsic first argument and result vector lengths must be equal",
6320 &FPI);
6321 }
6322 }
6323 break;
6324
6325 case Intrinsic::experimental_constrained_sitofp:
6326 case Intrinsic::experimental_constrained_uitofp: {
6327 Value *Operand = FPI.getArgOperand(0);
6328 ElementCount SrcEC;
6329 Check(Operand->getType()->isIntOrIntVectorTy(),
6330 "Intrinsic first argument must be integer", &FPI);
6331 if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
6332 SrcEC = cast<VectorType>(OperandT)->getElementCount();
6333 }
6334
6335 Operand = &FPI;
6336 Check(SrcEC.isNonZero() == Operand->getType()->isVectorTy(),
6337 "Intrinsic first argument and result disagree on vector use", &FPI);
6338 Check(Operand->getType()->isFPOrFPVectorTy(),
6339 "Intrinsic result must be a floating point", &FPI);
6340 if (auto *OperandT = dyn_cast<VectorType>(Operand->getType())) {
6341 Check(SrcEC == cast<VectorType>(OperandT)->getElementCount(),
6342 "Intrinsic first argument and result vector lengths must be equal",
6343 &FPI);
6344 }
6345 } break;
6346
6347 case Intrinsic::experimental_constrained_fptrunc:
6348 case Intrinsic::experimental_constrained_fpext: {
6349 Value *Operand = FPI.getArgOperand(0);
6350 Type *OperandTy = Operand->getType();
6351 Value *Result = &FPI;
6352 Type *ResultTy = Result->getType();
6353 Check(OperandTy->isFPOrFPVectorTy(),
6354 "Intrinsic first argument must be FP or FP vector", &FPI);
6355 Check(ResultTy->isFPOrFPVectorTy(),
6356 "Intrinsic result must be FP or FP vector", &FPI);
6357 Check(OperandTy->isVectorTy() == ResultTy->isVectorTy(),
6358 "Intrinsic first argument and result disagree on vector use", &FPI);
6359 if (OperandTy->isVectorTy()) {
6360 Check(cast<VectorType>(OperandTy)->getElementCount() ==
6361 cast<VectorType>(ResultTy)->getElementCount(),
6362 "Intrinsic first argument and result vector lengths must be equal",
6363 &FPI);
6364 }
6365 if (FPI.getIntrinsicID() == Intrinsic::experimental_constrained_fptrunc) {
6366 Check(OperandTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits(),
6367 "Intrinsic first argument's type must be larger than result type",
6368 &FPI);
6369 } else {
6370 Check(OperandTy->getScalarSizeInBits() < ResultTy->getScalarSizeInBits(),
6371 "Intrinsic first argument's type must be smaller than result type",
6372 &FPI);
6373 }
6374 }
6375 break;
6376
6377 default:
6378 break;
6379 }
6380
6381 // If a non-metadata argument is passed in a metadata slot then the
6382 // error will be caught earlier when the incorrect argument doesn't
6383 // match the specification in the intrinsic call table. Thus, no
6384 // argument type check is needed here.
6385
6386 Check(FPI.getExceptionBehavior().has_value(),
6387 "invalid exception behavior argument", &FPI);
6388 if (HasRoundingMD) {
6389 Check(FPI.getRoundingMode().has_value(), "invalid rounding mode argument",
6390 &FPI);
6391 }
6392 }
6393
visitDbgIntrinsic(StringRef Kind,DbgVariableIntrinsic & DII)6394 void Verifier::visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII) {
6395 auto *MD = DII.getRawLocation();
6396 CheckDI(isa<ValueAsMetadata>(MD) || isa<DIArgList>(MD) ||
6397 (isa<MDNode>(MD) && !cast<MDNode>(MD)->getNumOperands()),
6398 "invalid llvm.dbg." + Kind + " intrinsic address/value", &DII, MD);
6399 CheckDI(isa<DILocalVariable>(DII.getRawVariable()),
6400 "invalid llvm.dbg." + Kind + " intrinsic variable", &DII,
6401 DII.getRawVariable());
6402 CheckDI(isa<DIExpression>(DII.getRawExpression()),
6403 "invalid llvm.dbg." + Kind + " intrinsic expression", &DII,
6404 DII.getRawExpression());
6405
6406 if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&DII)) {
6407 CheckDI(isa<DIAssignID>(DAI->getRawAssignID()),
6408 "invalid llvm.dbg.assign intrinsic DIAssignID", &DII,
6409 DAI->getRawAssignID());
6410 const auto *RawAddr = DAI->getRawAddress();
6411 CheckDI(
6412 isa<ValueAsMetadata>(RawAddr) ||
6413 (isa<MDNode>(RawAddr) && !cast<MDNode>(RawAddr)->getNumOperands()),
6414 "invalid llvm.dbg.assign intrinsic address", &DII,
6415 DAI->getRawAddress());
6416 CheckDI(isa<DIExpression>(DAI->getRawAddressExpression()),
6417 "invalid llvm.dbg.assign intrinsic address expression", &DII,
6418 DAI->getRawAddressExpression());
6419 // All of the linked instructions should be in the same function as DII.
6420 for (Instruction *I : at::getAssignmentInsts(DAI))
6421 CheckDI(DAI->getFunction() == I->getFunction(),
6422 "inst not in same function as dbg.assign", I, DAI);
6423 }
6424
6425 // Ignore broken !dbg attachments; they're checked elsewhere.
6426 if (MDNode *N = DII.getDebugLoc().getAsMDNode())
6427 if (!isa<DILocation>(N))
6428 return;
6429
6430 BasicBlock *BB = DII.getParent();
6431 Function *F = BB ? BB->getParent() : nullptr;
6432
6433 // The scopes for variables and !dbg attachments must agree.
6434 DILocalVariable *Var = DII.getVariable();
6435 DILocation *Loc = DII.getDebugLoc();
6436 CheckDI(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment",
6437 &DII, BB, F);
6438
6439 DISubprogram *VarSP = getSubprogram(Var->getRawScope());
6440 DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
6441 if (!VarSP || !LocSP)
6442 return; // Broken scope chains are checked elsewhere.
6443
6444 CheckDI(VarSP == LocSP,
6445 "mismatched subprogram between llvm.dbg." + Kind +
6446 " variable and !dbg attachment",
6447 &DII, BB, F, Var, Var->getScope()->getSubprogram(), Loc,
6448 Loc->getScope()->getSubprogram());
6449
6450 // This check is redundant with one in visitLocalVariable().
6451 CheckDI(isType(Var->getRawType()), "invalid type ref", Var,
6452 Var->getRawType());
6453 verifyFnArgs(DII);
6454 }
6455
visitDbgLabelIntrinsic(StringRef Kind,DbgLabelInst & DLI)6456 void Verifier::visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI) {
6457 CheckDI(isa<DILabel>(DLI.getRawLabel()),
6458 "invalid llvm.dbg." + Kind + " intrinsic variable", &DLI,
6459 DLI.getRawLabel());
6460
6461 // Ignore broken !dbg attachments; they're checked elsewhere.
6462 if (MDNode *N = DLI.getDebugLoc().getAsMDNode())
6463 if (!isa<DILocation>(N))
6464 return;
6465
6466 BasicBlock *BB = DLI.getParent();
6467 Function *F = BB ? BB->getParent() : nullptr;
6468
6469 // The scopes for variables and !dbg attachments must agree.
6470 DILabel *Label = DLI.getLabel();
6471 DILocation *Loc = DLI.getDebugLoc();
6472 Check(Loc, "llvm.dbg." + Kind + " intrinsic requires a !dbg attachment", &DLI,
6473 BB, F);
6474
6475 DISubprogram *LabelSP = getSubprogram(Label->getRawScope());
6476 DISubprogram *LocSP = getSubprogram(Loc->getRawScope());
6477 if (!LabelSP || !LocSP)
6478 return;
6479
6480 CheckDI(LabelSP == LocSP,
6481 "mismatched subprogram between llvm.dbg." + Kind +
6482 " label and !dbg attachment",
6483 &DLI, BB, F, Label, Label->getScope()->getSubprogram(), Loc,
6484 Loc->getScope()->getSubprogram());
6485 }
6486
verifyFragmentExpression(const DbgVariableIntrinsic & I)6487 void Verifier::verifyFragmentExpression(const DbgVariableIntrinsic &I) {
6488 DILocalVariable *V = dyn_cast_or_null<DILocalVariable>(I.getRawVariable());
6489 DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
6490
6491 // We don't know whether this intrinsic verified correctly.
6492 if (!V || !E || !E->isValid())
6493 return;
6494
6495 // Nothing to do if this isn't a DW_OP_LLVM_fragment expression.
6496 auto Fragment = E->getFragmentInfo();
6497 if (!Fragment)
6498 return;
6499
6500 // The frontend helps out GDB by emitting the members of local anonymous
6501 // unions as artificial local variables with shared storage. When SROA splits
6502 // the storage for artificial local variables that are smaller than the entire
6503 // union, the overhang piece will be outside of the allotted space for the
6504 // variable and this check fails.
6505 // FIXME: Remove this check as soon as clang stops doing this; it hides bugs.
6506 if (V->isArtificial())
6507 return;
6508
6509 verifyFragmentExpression(*V, *Fragment, &I);
6510 }
6511
6512 template <typename ValueOrMetadata>
verifyFragmentExpression(const DIVariable & V,DIExpression::FragmentInfo Fragment,ValueOrMetadata * Desc)6513 void Verifier::verifyFragmentExpression(const DIVariable &V,
6514 DIExpression::FragmentInfo Fragment,
6515 ValueOrMetadata *Desc) {
6516 // If there's no size, the type is broken, but that should be checked
6517 // elsewhere.
6518 auto VarSize = V.getSizeInBits();
6519 if (!VarSize)
6520 return;
6521
6522 unsigned FragSize = Fragment.SizeInBits;
6523 unsigned FragOffset = Fragment.OffsetInBits;
6524 CheckDI(FragSize + FragOffset <= *VarSize,
6525 "fragment is larger than or outside of variable", Desc, &V);
6526 CheckDI(FragSize != *VarSize, "fragment covers entire variable", Desc, &V);
6527 }
6528
verifyFnArgs(const DbgVariableIntrinsic & I)6529 void Verifier::verifyFnArgs(const DbgVariableIntrinsic &I) {
6530 // This function does not take the scope of noninlined function arguments into
6531 // account. Don't run it if current function is nodebug, because it may
6532 // contain inlined debug intrinsics.
6533 if (!HasDebugInfo)
6534 return;
6535
6536 // For performance reasons only check non-inlined ones.
6537 if (I.getDebugLoc()->getInlinedAt())
6538 return;
6539
6540 DILocalVariable *Var = I.getVariable();
6541 CheckDI(Var, "dbg intrinsic without variable");
6542
6543 unsigned ArgNo = Var->getArg();
6544 if (!ArgNo)
6545 return;
6546
6547 // Verify there are no duplicate function argument debug info entries.
6548 // These will cause hard-to-debug assertions in the DWARF backend.
6549 if (DebugFnArgs.size() < ArgNo)
6550 DebugFnArgs.resize(ArgNo, nullptr);
6551
6552 auto *Prev = DebugFnArgs[ArgNo - 1];
6553 DebugFnArgs[ArgNo - 1] = Var;
6554 CheckDI(!Prev || (Prev == Var), "conflicting debug info for argument", &I,
6555 Prev, Var);
6556 }
6557
verifyNotEntryValue(const DbgVariableIntrinsic & I)6558 void Verifier::verifyNotEntryValue(const DbgVariableIntrinsic &I) {
6559 DIExpression *E = dyn_cast_or_null<DIExpression>(I.getRawExpression());
6560
6561 // We don't know whether this intrinsic verified correctly.
6562 if (!E || !E->isValid())
6563 return;
6564
6565 if (isa<ValueAsMetadata>(I.getRawLocation())) {
6566 Value *VarValue = I.getVariableLocationOp(0);
6567 if (isa<UndefValue>(VarValue) || isa<PoisonValue>(VarValue))
6568 return;
6569 // We allow EntryValues for swift async arguments, as they have an
6570 // ABI-guarantee to be turned into a specific register.
6571 if (auto *ArgLoc = dyn_cast_or_null<Argument>(VarValue);
6572 ArgLoc && ArgLoc->hasAttribute(Attribute::SwiftAsync))
6573 return;
6574 }
6575
6576 CheckDI(!E->isEntryValue(),
6577 "Entry values are only allowed in MIR unless they target a "
6578 "swiftasync Argument",
6579 &I);
6580 }
6581
verifyCompileUnits()6582 void Verifier::verifyCompileUnits() {
6583 // When more than one Module is imported into the same context, such as during
6584 // an LTO build before linking the modules, ODR type uniquing may cause types
6585 // to point to a different CU. This check does not make sense in this case.
6586 if (M.getContext().isODRUniquingDebugTypes())
6587 return;
6588 auto *CUs = M.getNamedMetadata("llvm.dbg.cu");
6589 SmallPtrSet<const Metadata *, 2> Listed;
6590 if (CUs)
6591 Listed.insert(CUs->op_begin(), CUs->op_end());
6592 for (const auto *CU : CUVisited)
6593 CheckDI(Listed.count(CU), "DICompileUnit not listed in llvm.dbg.cu", CU);
6594 CUVisited.clear();
6595 }
6596
verifyDeoptimizeCallingConvs()6597 void Verifier::verifyDeoptimizeCallingConvs() {
6598 if (DeoptimizeDeclarations.empty())
6599 return;
6600
6601 const Function *First = DeoptimizeDeclarations[0];
6602 for (const auto *F : ArrayRef(DeoptimizeDeclarations).slice(1)) {
6603 Check(First->getCallingConv() == F->getCallingConv(),
6604 "All llvm.experimental.deoptimize declarations must have the same "
6605 "calling convention",
6606 First, F);
6607 }
6608 }
6609
verifyAttachedCallBundle(const CallBase & Call,const OperandBundleUse & BU)6610 void Verifier::verifyAttachedCallBundle(const CallBase &Call,
6611 const OperandBundleUse &BU) {
6612 FunctionType *FTy = Call.getFunctionType();
6613
6614 Check((FTy->getReturnType()->isPointerTy() ||
6615 (Call.doesNotReturn() && FTy->getReturnType()->isVoidTy())),
6616 "a call with operand bundle \"clang.arc.attachedcall\" must call a "
6617 "function returning a pointer or a non-returning function that has a "
6618 "void return type",
6619 Call);
6620
6621 Check(BU.Inputs.size() == 1 && isa<Function>(BU.Inputs.front()),
6622 "operand bundle \"clang.arc.attachedcall\" requires one function as "
6623 "an argument",
6624 Call);
6625
6626 auto *Fn = cast<Function>(BU.Inputs.front());
6627 Intrinsic::ID IID = Fn->getIntrinsicID();
6628
6629 if (IID) {
6630 Check((IID == Intrinsic::objc_retainAutoreleasedReturnValue ||
6631 IID == Intrinsic::objc_unsafeClaimAutoreleasedReturnValue),
6632 "invalid function argument", Call);
6633 } else {
6634 StringRef FnName = Fn->getName();
6635 Check((FnName == "objc_retainAutoreleasedReturnValue" ||
6636 FnName == "objc_unsafeClaimAutoreleasedReturnValue"),
6637 "invalid function argument", Call);
6638 }
6639 }
6640
verifyNoAliasScopeDecl()6641 void Verifier::verifyNoAliasScopeDecl() {
6642 if (NoAliasScopeDecls.empty())
6643 return;
6644
6645 // only a single scope must be declared at a time.
6646 for (auto *II : NoAliasScopeDecls) {
6647 assert(II->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl &&
6648 "Not a llvm.experimental.noalias.scope.decl ?");
6649 const auto *ScopeListMV = dyn_cast<MetadataAsValue>(
6650 II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
6651 Check(ScopeListMV != nullptr,
6652 "llvm.experimental.noalias.scope.decl must have a MetadataAsValue "
6653 "argument",
6654 II);
6655
6656 const auto *ScopeListMD = dyn_cast<MDNode>(ScopeListMV->getMetadata());
6657 Check(ScopeListMD != nullptr, "!id.scope.list must point to an MDNode", II);
6658 Check(ScopeListMD->getNumOperands() == 1,
6659 "!id.scope.list must point to a list with a single scope", II);
6660 visitAliasScopeListMetadata(ScopeListMD);
6661 }
6662
6663 // Only check the domination rule when requested. Once all passes have been
6664 // adapted this option can go away.
6665 if (!VerifyNoAliasScopeDomination)
6666 return;
6667
6668 // Now sort the intrinsics based on the scope MDNode so that declarations of
6669 // the same scopes are next to each other.
6670 auto GetScope = [](IntrinsicInst *II) {
6671 const auto *ScopeListMV = cast<MetadataAsValue>(
6672 II->getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
6673 return &cast<MDNode>(ScopeListMV->getMetadata())->getOperand(0);
6674 };
6675
6676 // We are sorting on MDNode pointers here. For valid input IR this is ok.
6677 // TODO: Sort on Metadata ID to avoid non-deterministic error messages.
6678 auto Compare = [GetScope](IntrinsicInst *Lhs, IntrinsicInst *Rhs) {
6679 return GetScope(Lhs) < GetScope(Rhs);
6680 };
6681
6682 llvm::sort(NoAliasScopeDecls, Compare);
6683
6684 // Go over the intrinsics and check that for the same scope, they are not
6685 // dominating each other.
6686 auto ItCurrent = NoAliasScopeDecls.begin();
6687 while (ItCurrent != NoAliasScopeDecls.end()) {
6688 auto CurScope = GetScope(*ItCurrent);
6689 auto ItNext = ItCurrent;
6690 do {
6691 ++ItNext;
6692 } while (ItNext != NoAliasScopeDecls.end() &&
6693 GetScope(*ItNext) == CurScope);
6694
6695 // [ItCurrent, ItNext) represents the declarations for the same scope.
6696 // Ensure they are not dominating each other.. but only if it is not too
6697 // expensive.
6698 if (ItNext - ItCurrent < 32)
6699 for (auto *I : llvm::make_range(ItCurrent, ItNext))
6700 for (auto *J : llvm::make_range(ItCurrent, ItNext))
6701 if (I != J)
6702 Check(!DT.dominates(I, J),
6703 "llvm.experimental.noalias.scope.decl dominates another one "
6704 "with the same scope",
6705 I);
6706 ItCurrent = ItNext;
6707 }
6708 }
6709
6710 //===----------------------------------------------------------------------===//
6711 // Implement the public interfaces to this file...
6712 //===----------------------------------------------------------------------===//
6713
verifyFunction(const Function & f,raw_ostream * OS)6714 bool llvm::verifyFunction(const Function &f, raw_ostream *OS) {
6715 Function &F = const_cast<Function &>(f);
6716
6717 // Don't use a raw_null_ostream. Printing IR is expensive.
6718 Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/true, *f.getParent());
6719
6720 // Note that this function's return value is inverted from what you would
6721 // expect of a function called "verify".
6722 return !V.verify(F);
6723 }
6724
verifyModule(const Module & M,raw_ostream * OS,bool * BrokenDebugInfo)6725 bool llvm::verifyModule(const Module &M, raw_ostream *OS,
6726 bool *BrokenDebugInfo) {
6727 // Don't use a raw_null_ostream. Printing IR is expensive.
6728 Verifier V(OS, /*ShouldTreatBrokenDebugInfoAsError=*/!BrokenDebugInfo, M);
6729
6730 bool Broken = false;
6731 for (const Function &F : M)
6732 Broken |= !V.verify(F);
6733
6734 Broken |= !V.verify();
6735 if (BrokenDebugInfo)
6736 *BrokenDebugInfo = V.hasBrokenDebugInfo();
6737 // Note that this function's return value is inverted from what you would
6738 // expect of a function called "verify".
6739 return Broken;
6740 }
6741
6742 namespace {
6743
6744 struct VerifierLegacyPass : public FunctionPass {
6745 static char ID;
6746
6747 std::unique_ptr<Verifier> V;
6748 bool FatalErrors = true;
6749
VerifierLegacyPass__anoneaa63fbe1311::VerifierLegacyPass6750 VerifierLegacyPass() : FunctionPass(ID) {
6751 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
6752 }
VerifierLegacyPass__anoneaa63fbe1311::VerifierLegacyPass6753 explicit VerifierLegacyPass(bool FatalErrors)
6754 : FunctionPass(ID),
6755 FatalErrors(FatalErrors) {
6756 initializeVerifierLegacyPassPass(*PassRegistry::getPassRegistry());
6757 }
6758
doInitialization__anoneaa63fbe1311::VerifierLegacyPass6759 bool doInitialization(Module &M) override {
6760 V = std::make_unique<Verifier>(
6761 &dbgs(), /*ShouldTreatBrokenDebugInfoAsError=*/false, M);
6762 return false;
6763 }
6764
runOnFunction__anoneaa63fbe1311::VerifierLegacyPass6765 bool runOnFunction(Function &F) override {
6766 if (!V->verify(F) && FatalErrors) {
6767 errs() << "in function " << F.getName() << '\n';
6768 report_fatal_error("Broken function found, compilation aborted!");
6769 }
6770 return false;
6771 }
6772
doFinalization__anoneaa63fbe1311::VerifierLegacyPass6773 bool doFinalization(Module &M) override {
6774 bool HasErrors = false;
6775 for (Function &F : M)
6776 if (F.isDeclaration())
6777 HasErrors |= !V->verify(F);
6778
6779 HasErrors |= !V->verify();
6780 if (FatalErrors && (HasErrors || V->hasBrokenDebugInfo()))
6781 report_fatal_error("Broken module found, compilation aborted!");
6782 return false;
6783 }
6784
getAnalysisUsage__anoneaa63fbe1311::VerifierLegacyPass6785 void getAnalysisUsage(AnalysisUsage &AU) const override {
6786 AU.setPreservesAll();
6787 }
6788 };
6789
6790 } // end anonymous namespace
6791
6792 /// Helper to issue failure from the TBAA verification
CheckFailed(Tys &&...Args)6793 template <typename... Tys> void TBAAVerifier::CheckFailed(Tys &&... Args) {
6794 if (Diagnostic)
6795 return Diagnostic->CheckFailed(Args...);
6796 }
6797
6798 #define CheckTBAA(C, ...) \
6799 do { \
6800 if (!(C)) { \
6801 CheckFailed(__VA_ARGS__); \
6802 return false; \
6803 } \
6804 } while (false)
6805
6806 /// Verify that \p BaseNode can be used as the "base type" in the struct-path
6807 /// TBAA scheme. This means \p BaseNode is either a scalar node, or a
6808 /// struct-type node describing an aggregate data structure (like a struct).
6809 TBAAVerifier::TBAABaseNodeSummary
verifyTBAABaseNode(Instruction & I,const MDNode * BaseNode,bool IsNewFormat)6810 TBAAVerifier::verifyTBAABaseNode(Instruction &I, const MDNode *BaseNode,
6811 bool IsNewFormat) {
6812 if (BaseNode->getNumOperands() < 2) {
6813 CheckFailed("Base nodes must have at least two operands", &I, BaseNode);
6814 return {true, ~0u};
6815 }
6816
6817 auto Itr = TBAABaseNodes.find(BaseNode);
6818 if (Itr != TBAABaseNodes.end())
6819 return Itr->second;
6820
6821 auto Result = verifyTBAABaseNodeImpl(I, BaseNode, IsNewFormat);
6822 auto InsertResult = TBAABaseNodes.insert({BaseNode, Result});
6823 (void)InsertResult;
6824 assert(InsertResult.second && "We just checked!");
6825 return Result;
6826 }
6827
6828 TBAAVerifier::TBAABaseNodeSummary
verifyTBAABaseNodeImpl(Instruction & I,const MDNode * BaseNode,bool IsNewFormat)6829 TBAAVerifier::verifyTBAABaseNodeImpl(Instruction &I, const MDNode *BaseNode,
6830 bool IsNewFormat) {
6831 const TBAAVerifier::TBAABaseNodeSummary InvalidNode = {true, ~0u};
6832
6833 if (BaseNode->getNumOperands() == 2) {
6834 // Scalar nodes can only be accessed at offset 0.
6835 return isValidScalarTBAANode(BaseNode)
6836 ? TBAAVerifier::TBAABaseNodeSummary({false, 0})
6837 : InvalidNode;
6838 }
6839
6840 if (IsNewFormat) {
6841 if (BaseNode->getNumOperands() % 3 != 0) {
6842 CheckFailed("Access tag nodes must have the number of operands that is a "
6843 "multiple of 3!", BaseNode);
6844 return InvalidNode;
6845 }
6846 } else {
6847 if (BaseNode->getNumOperands() % 2 != 1) {
6848 CheckFailed("Struct tag nodes must have an odd number of operands!",
6849 BaseNode);
6850 return InvalidNode;
6851 }
6852 }
6853
6854 // Check the type size field.
6855 if (IsNewFormat) {
6856 auto *TypeSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
6857 BaseNode->getOperand(1));
6858 if (!TypeSizeNode) {
6859 CheckFailed("Type size nodes must be constants!", &I, BaseNode);
6860 return InvalidNode;
6861 }
6862 }
6863
6864 // Check the type name field. In the new format it can be anything.
6865 if (!IsNewFormat && !isa<MDString>(BaseNode->getOperand(0))) {
6866 CheckFailed("Struct tag nodes have a string as their first operand",
6867 BaseNode);
6868 return InvalidNode;
6869 }
6870
6871 bool Failed = false;
6872
6873 std::optional<APInt> PrevOffset;
6874 unsigned BitWidth = ~0u;
6875
6876 // We've already checked that BaseNode is not a degenerate root node with one
6877 // operand in \c verifyTBAABaseNode, so this loop should run at least once.
6878 unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
6879 unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
6880 for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
6881 Idx += NumOpsPerField) {
6882 const MDOperand &FieldTy = BaseNode->getOperand(Idx);
6883 const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1);
6884 if (!isa<MDNode>(FieldTy)) {
6885 CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode);
6886 Failed = true;
6887 continue;
6888 }
6889
6890 auto *OffsetEntryCI =
6891 mdconst::dyn_extract_or_null<ConstantInt>(FieldOffset);
6892 if (!OffsetEntryCI) {
6893 CheckFailed("Offset entries must be constants!", &I, BaseNode);
6894 Failed = true;
6895 continue;
6896 }
6897
6898 if (BitWidth == ~0u)
6899 BitWidth = OffsetEntryCI->getBitWidth();
6900
6901 if (OffsetEntryCI->getBitWidth() != BitWidth) {
6902 CheckFailed(
6903 "Bitwidth between the offsets and struct type entries must match", &I,
6904 BaseNode);
6905 Failed = true;
6906 continue;
6907 }
6908
6909 // NB! As far as I can tell, we generate a non-strictly increasing offset
6910 // sequence only from structs that have zero size bit fields. When
6911 // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
6912 // pick the field lexically the latest in struct type metadata node. This
6913 // mirrors the actual behavior of the alias analysis implementation.
6914 bool IsAscending =
6915 !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue());
6916
6917 if (!IsAscending) {
6918 CheckFailed("Offsets must be increasing!", &I, BaseNode);
6919 Failed = true;
6920 }
6921
6922 PrevOffset = OffsetEntryCI->getValue();
6923
6924 if (IsNewFormat) {
6925 auto *MemberSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
6926 BaseNode->getOperand(Idx + 2));
6927 if (!MemberSizeNode) {
6928 CheckFailed("Member size entries must be constants!", &I, BaseNode);
6929 Failed = true;
6930 continue;
6931 }
6932 }
6933 }
6934
6935 return Failed ? InvalidNode
6936 : TBAAVerifier::TBAABaseNodeSummary(false, BitWidth);
6937 }
6938
IsRootTBAANode(const MDNode * MD)6939 static bool IsRootTBAANode(const MDNode *MD) {
6940 return MD->getNumOperands() < 2;
6941 }
6942
IsScalarTBAANodeImpl(const MDNode * MD,SmallPtrSetImpl<const MDNode * > & Visited)6943 static bool IsScalarTBAANodeImpl(const MDNode *MD,
6944 SmallPtrSetImpl<const MDNode *> &Visited) {
6945 if (MD->getNumOperands() != 2 && MD->getNumOperands() != 3)
6946 return false;
6947
6948 if (!isa<MDString>(MD->getOperand(0)))
6949 return false;
6950
6951 if (MD->getNumOperands() == 3) {
6952 auto *Offset = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
6953 if (!(Offset && Offset->isZero() && isa<MDString>(MD->getOperand(0))))
6954 return false;
6955 }
6956
6957 auto *Parent = dyn_cast_or_null<MDNode>(MD->getOperand(1));
6958 return Parent && Visited.insert(Parent).second &&
6959 (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited));
6960 }
6961
isValidScalarTBAANode(const MDNode * MD)6962 bool TBAAVerifier::isValidScalarTBAANode(const MDNode *MD) {
6963 auto ResultIt = TBAAScalarNodes.find(MD);
6964 if (ResultIt != TBAAScalarNodes.end())
6965 return ResultIt->second;
6966
6967 SmallPtrSet<const MDNode *, 4> Visited;
6968 bool Result = IsScalarTBAANodeImpl(MD, Visited);
6969 auto InsertResult = TBAAScalarNodes.insert({MD, Result});
6970 (void)InsertResult;
6971 assert(InsertResult.second && "Just checked!");
6972
6973 return Result;
6974 }
6975
6976 /// Returns the field node at the offset \p Offset in \p BaseNode. Update \p
6977 /// Offset in place to be the offset within the field node returned.
6978 ///
6979 /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
getFieldNodeFromTBAABaseNode(Instruction & I,const MDNode * BaseNode,APInt & Offset,bool IsNewFormat)6980 MDNode *TBAAVerifier::getFieldNodeFromTBAABaseNode(Instruction &I,
6981 const MDNode *BaseNode,
6982 APInt &Offset,
6983 bool IsNewFormat) {
6984 assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!");
6985
6986 // Scalar nodes have only one possible "field" -- their parent in the access
6987 // hierarchy. Offset must be zero at this point, but our caller is supposed
6988 // to check that.
6989 if (BaseNode->getNumOperands() == 2)
6990 return cast<MDNode>(BaseNode->getOperand(1));
6991
6992 unsigned FirstFieldOpNo = IsNewFormat ? 3 : 1;
6993 unsigned NumOpsPerField = IsNewFormat ? 3 : 2;
6994 for (unsigned Idx = FirstFieldOpNo; Idx < BaseNode->getNumOperands();
6995 Idx += NumOpsPerField) {
6996 auto *OffsetEntryCI =
6997 mdconst::extract<ConstantInt>(BaseNode->getOperand(Idx + 1));
6998 if (OffsetEntryCI->getValue().ugt(Offset)) {
6999 if (Idx == FirstFieldOpNo) {
7000 CheckFailed("Could not find TBAA parent in struct type node", &I,
7001 BaseNode, &Offset);
7002 return nullptr;
7003 }
7004
7005 unsigned PrevIdx = Idx - NumOpsPerField;
7006 auto *PrevOffsetEntryCI =
7007 mdconst::extract<ConstantInt>(BaseNode->getOperand(PrevIdx + 1));
7008 Offset -= PrevOffsetEntryCI->getValue();
7009 return cast<MDNode>(BaseNode->getOperand(PrevIdx));
7010 }
7011 }
7012
7013 unsigned LastIdx = BaseNode->getNumOperands() - NumOpsPerField;
7014 auto *LastOffsetEntryCI = mdconst::extract<ConstantInt>(
7015 BaseNode->getOperand(LastIdx + 1));
7016 Offset -= LastOffsetEntryCI->getValue();
7017 return cast<MDNode>(BaseNode->getOperand(LastIdx));
7018 }
7019
isNewFormatTBAATypeNode(llvm::MDNode * Type)7020 static bool isNewFormatTBAATypeNode(llvm::MDNode *Type) {
7021 if (!Type || Type->getNumOperands() < 3)
7022 return false;
7023
7024 // In the new format type nodes shall have a reference to the parent type as
7025 // its first operand.
7026 return isa_and_nonnull<MDNode>(Type->getOperand(0));
7027 }
7028
visitTBAAMetadata(Instruction & I,const MDNode * MD)7029 bool TBAAVerifier::visitTBAAMetadata(Instruction &I, const MDNode *MD) {
7030 CheckTBAA(MD->getNumOperands() > 0, "TBAA metadata cannot have 0 operands",
7031 &I, MD);
7032
7033 CheckTBAA(isa<LoadInst>(I) || isa<StoreInst>(I) || isa<CallInst>(I) ||
7034 isa<VAArgInst>(I) || isa<AtomicRMWInst>(I) ||
7035 isa<AtomicCmpXchgInst>(I),
7036 "This instruction shall not have a TBAA access tag!", &I);
7037
7038 bool IsStructPathTBAA =
7039 isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
7040
7041 CheckTBAA(IsStructPathTBAA,
7042 "Old-style TBAA is no longer allowed, use struct-path TBAA instead",
7043 &I);
7044
7045 MDNode *BaseNode = dyn_cast_or_null<MDNode>(MD->getOperand(0));
7046 MDNode *AccessType = dyn_cast_or_null<MDNode>(MD->getOperand(1));
7047
7048 bool IsNewFormat = isNewFormatTBAATypeNode(AccessType);
7049
7050 if (IsNewFormat) {
7051 CheckTBAA(MD->getNumOperands() == 4 || MD->getNumOperands() == 5,
7052 "Access tag metadata must have either 4 or 5 operands", &I, MD);
7053 } else {
7054 CheckTBAA(MD->getNumOperands() < 5,
7055 "Struct tag metadata must have either 3 or 4 operands", &I, MD);
7056 }
7057
7058 // Check the access size field.
7059 if (IsNewFormat) {
7060 auto *AccessSizeNode = mdconst::dyn_extract_or_null<ConstantInt>(
7061 MD->getOperand(3));
7062 CheckTBAA(AccessSizeNode, "Access size field must be a constant", &I, MD);
7063 }
7064
7065 // Check the immutability flag.
7066 unsigned ImmutabilityFlagOpNo = IsNewFormat ? 4 : 3;
7067 if (MD->getNumOperands() == ImmutabilityFlagOpNo + 1) {
7068 auto *IsImmutableCI = mdconst::dyn_extract_or_null<ConstantInt>(
7069 MD->getOperand(ImmutabilityFlagOpNo));
7070 CheckTBAA(IsImmutableCI,
7071 "Immutability tag on struct tag metadata must be a constant", &I,
7072 MD);
7073 CheckTBAA(
7074 IsImmutableCI->isZero() || IsImmutableCI->isOne(),
7075 "Immutability part of the struct tag metadata must be either 0 or 1",
7076 &I, MD);
7077 }
7078
7079 CheckTBAA(BaseNode && AccessType,
7080 "Malformed struct tag metadata: base and access-type "
7081 "should be non-null and point to Metadata nodes",
7082 &I, MD, BaseNode, AccessType);
7083
7084 if (!IsNewFormat) {
7085 CheckTBAA(isValidScalarTBAANode(AccessType),
7086 "Access type node must be a valid scalar type", &I, MD,
7087 AccessType);
7088 }
7089
7090 auto *OffsetCI = mdconst::dyn_extract_or_null<ConstantInt>(MD->getOperand(2));
7091 CheckTBAA(OffsetCI, "Offset must be constant integer", &I, MD);
7092
7093 APInt Offset = OffsetCI->getValue();
7094 bool SeenAccessTypeInPath = false;
7095
7096 SmallPtrSet<MDNode *, 4> StructPath;
7097
7098 for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode);
7099 BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset,
7100 IsNewFormat)) {
7101 if (!StructPath.insert(BaseNode).second) {
7102 CheckFailed("Cycle detected in struct path", &I, MD);
7103 return false;
7104 }
7105
7106 bool Invalid;
7107 unsigned BaseNodeBitWidth;
7108 std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode,
7109 IsNewFormat);
7110
7111 // If the base node is invalid in itself, then we've already printed all the
7112 // errors we wanted to print.
7113 if (Invalid)
7114 return false;
7115
7116 SeenAccessTypeInPath |= BaseNode == AccessType;
7117
7118 if (isValidScalarTBAANode(BaseNode) || BaseNode == AccessType)
7119 CheckTBAA(Offset == 0, "Offset not zero at the point of scalar access",
7120 &I, MD, &Offset);
7121
7122 CheckTBAA(BaseNodeBitWidth == Offset.getBitWidth() ||
7123 (BaseNodeBitWidth == 0 && Offset == 0) ||
7124 (IsNewFormat && BaseNodeBitWidth == ~0u),
7125 "Access bit-width not the same as description bit-width", &I, MD,
7126 BaseNodeBitWidth, Offset.getBitWidth());
7127
7128 if (IsNewFormat && SeenAccessTypeInPath)
7129 break;
7130 }
7131
7132 CheckTBAA(SeenAccessTypeInPath, "Did not see access type in access path!", &I,
7133 MD);
7134 return true;
7135 }
7136
7137 char VerifierLegacyPass::ID = 0;
7138 INITIALIZE_PASS(VerifierLegacyPass, "verify", "Module Verifier", false, false)
7139
createVerifierPass(bool FatalErrors)7140 FunctionPass *llvm::createVerifierPass(bool FatalErrors) {
7141 return new VerifierLegacyPass(FatalErrors);
7142 }
7143
7144 AnalysisKey VerifierAnalysis::Key;
run(Module & M,ModuleAnalysisManager &)7145 VerifierAnalysis::Result VerifierAnalysis::run(Module &M,
7146 ModuleAnalysisManager &) {
7147 Result Res;
7148 Res.IRBroken = llvm::verifyModule(M, &dbgs(), &Res.DebugInfoBroken);
7149 return Res;
7150 }
7151
run(Function & F,FunctionAnalysisManager &)7152 VerifierAnalysis::Result VerifierAnalysis::run(Function &F,
7153 FunctionAnalysisManager &) {
7154 return { llvm::verifyFunction(F, &dbgs()), false };
7155 }
7156
run(Module & M,ModuleAnalysisManager & AM)7157 PreservedAnalyses VerifierPass::run(Module &M, ModuleAnalysisManager &AM) {
7158 auto Res = AM.getResult<VerifierAnalysis>(M);
7159 if (FatalErrors && (Res.IRBroken || Res.DebugInfoBroken))
7160 report_fatal_error("Broken module found, compilation aborted!");
7161
7162 return PreservedAnalyses::all();
7163 }
7164
run(Function & F,FunctionAnalysisManager & AM)7165 PreservedAnalyses VerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
7166 auto res = AM.getResult<VerifierAnalysis>(F);
7167 if (res.IRBroken && FatalErrors)
7168 report_fatal_error("Broken function found, compilation aborted!");
7169
7170 return PreservedAnalyses::all();
7171 }
7172