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