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