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