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