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