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