LLVM  14.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 // sanity 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 // * PHI nodes must have at least one entry
27 // * All basic blocks should only end with terminator insts, not contain them
28 // * The entry node to a function must not have predecessors
29 // * All Instructions must be embedded into a basic block
30 // * Functions cannot take a void-typed parameter
31 // * Verify that a function's argument list agrees with it's declared type.
32 // * It is illegal to specify a name for a void value.
33 // * It is illegal to have a internal global value with no initializer
34 // * It is illegal to have a ret instruction that returns a value that does not
35 // agree with the function return value type.
36 // * Function call argument types match the function prototype
37 // * A landing pad is defined by a landingpad instruction, and can be jumped to
38 // only by the unwind edge of an invoke instruction.
39 // * A landingpad instruction must be the first non-PHI instruction in the
40 // block.
41 // * Landingpad instructions must be in a function with a personality function.
42 // * All other things that are tested by asserts spread about the code...
43 //
44 //===----------------------------------------------------------------------===//
45 
46 #include "llvm/IR/Verifier.h"
47 #include "llvm/ADT/APFloat.h"
48 #include "llvm/ADT/APInt.h"
49 #include "llvm/ADT/ArrayRef.h"
50 #include "llvm/ADT/DenseMap.h"
51 #include "llvm/ADT/MapVector.h"
52 #include "llvm/ADT/Optional.h"
53 #include "llvm/ADT/STLExtras.h"
54 #include "llvm/ADT/SmallPtrSet.h"
55 #include "llvm/ADT/SmallSet.h"
56 #include "llvm/ADT/SmallVector.h"
57 #include "llvm/ADT/StringExtras.h"
58 #include "llvm/ADT/StringMap.h"
59 #include "llvm/ADT/StringRef.h"
60 #include "llvm/ADT/Twine.h"
61 #include "llvm/ADT/ilist.h"
63 #include "llvm/IR/Argument.h"
64 #include "llvm/IR/Attributes.h"
65 #include "llvm/IR/BasicBlock.h"
66 #include "llvm/IR/CFG.h"
67 #include "llvm/IR/CallingConv.h"
68 #include "llvm/IR/Comdat.h"
69 #include "llvm/IR/Constant.h"
70 #include "llvm/IR/ConstantRange.h"
71 #include "llvm/IR/Constants.h"
72 #include "llvm/IR/DataLayout.h"
73 #include "llvm/IR/DebugInfo.h"
75 #include "llvm/IR/DebugLoc.h"
76 #include "llvm/IR/DerivedTypes.h"
77 #include "llvm/IR/Dominators.h"
78 #include "llvm/IR/Function.h"
79 #include "llvm/IR/GlobalAlias.h"
80 #include "llvm/IR/GlobalValue.h"
81 #include "llvm/IR/GlobalVariable.h"
82 #include "llvm/IR/InlineAsm.h"
83 #include "llvm/IR/InstVisitor.h"
84 #include "llvm/IR/InstrTypes.h"
85 #include "llvm/IR/Instruction.h"
86 #include "llvm/IR/Instructions.h"
87 #include "llvm/IR/IntrinsicInst.h"
88 #include "llvm/IR/Intrinsics.h"
89 #include "llvm/IR/IntrinsicsWebAssembly.h"
90 #include "llvm/IR/LLVMContext.h"
91 #include "llvm/IR/Metadata.h"
92 #include "llvm/IR/Module.h"
94 #include "llvm/IR/PassManager.h"
95 #include "llvm/IR/Statepoint.h"
96 #include "llvm/IR/Type.h"
97 #include "llvm/IR/Use.h"
98 #include "llvm/IR/User.h"
99 #include "llvm/IR/Value.h"
100 #include "llvm/InitializePasses.h"
101 #include "llvm/Pass.h"
103 #include "llvm/Support/Casting.h"
105 #include "llvm/Support/Debug.h"
107 #include "llvm/Support/MathExtras.h"
109 #include <algorithm>
110 #include <cassert>
111 #include <cstdint>
112 #include <memory>
113 #include <string>
114 #include <utility>
115 
116 using namespace llvm;
117 
119  "verify-noalias-scope-decl-dom", cl::Hidden, cl::init(false),
120  cl::desc("Ensure that llvm.experimental.noalias.scope.decl for identical "
121  "scopes are not dominating"));
122 
123 namespace llvm {
124 
127  const Module &M;
130  const DataLayout &DL;
132 
133  /// Track the brokenness of the module while recursively visiting.
134  bool Broken = false;
135  /// Broken debug info can be "recovered" from by stripping the debug info.
136  bool BrokenDebugInfo = false;
137  /// Whether to treat broken debug info as an error.
139 
140  explicit VerifierSupport(raw_ostream *OS, const Module &M)
141  : OS(OS), M(M), MST(&M), TT(M.getTargetTriple()), DL(M.getDataLayout()),
142  Context(M.getContext()) {}
143 
144 private:
145  void Write(const Module *M) {
146  *OS << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
147  }
148 
149  void Write(const Value *V) {
150  if (V)
151  Write(*V);
152  }
153 
154  void Write(const Value &V) {
155  if (isa<Instruction>(V)) {
156  V.print(*OS, MST);
157  *OS << '\n';
158  } else {
159  V.printAsOperand(*OS, true, MST);
160  *OS << '\n';
161  }
162  }
163 
164  void Write(const Metadata *MD) {
165  if (!MD)
166  return;
167  MD->print(*OS, MST, &M);
168  *OS << '\n';
169  }
170 
171  template <class T> void Write(const MDTupleTypedArrayWrapper<T> &MD) {
172  Write(MD.get());
173  }
174 
175  void Write(const NamedMDNode *NMD) {
176  if (!NMD)
177  return;
178  NMD->print(*OS, MST);
179  *OS << '\n';
180  }
181 
182  void Write(Type *T) {
183  if (!T)
184  return;
185  *OS << ' ' << *T;
186  }
187 
188  void Write(const Comdat *C) {
189  if (!C)
190  return;
191  *OS << *C;
192  }
193 
194  void Write(const APInt *AI) {
195  if (!AI)
196  return;
197  *OS << *AI << '\n';
198  }
199 
200  void Write(const unsigned i) { *OS << i << '\n'; }
201 
202  // NOLINTNEXTLINE(readability-identifier-naming)
203  void Write(const Attribute *A) {
204  if (!A)
205  return;
206  *OS << A->getAsString() << '\n';
207  }
208 
209  // NOLINTNEXTLINE(readability-identifier-naming)
210  void Write(const AttributeSet *AS) {
211  if (!AS)
212  return;
213  *OS << AS->getAsString() << '\n';
214  }
215 
216  // NOLINTNEXTLINE(readability-identifier-naming)
217  void Write(const AttributeList *AL) {
218  if (!AL)
219  return;
220  AL->print(*OS);
221  }
222 
223  template <typename T> void Write(ArrayRef<T> Vs) {
224  for (const T &V : Vs)
225  Write(V);
226  }
227 
228  template <typename T1, typename... Ts>
229  void WriteTs(const T1 &V1, const Ts &... Vs) {
230  Write(V1);
231  WriteTs(Vs...);
232  }
233 
234  template <typename... Ts> void WriteTs() {}
235 
236 public:
237  /// A check failed, so printout out the condition and the message.
238  ///
239  /// This provides a nice place to put a breakpoint if you want to see why
240  /// something is not correct.
241  void CheckFailed(const Twine &Message) {
242  if (OS)
243  *OS << Message << '\n';
244  Broken = true;
245  }
246 
247  /// A check failed (with values to print).
248  ///
249  /// This calls the Message-only version so that the above is easier to set a
250  /// breakpoint on.
251  template <typename T1, typename... Ts>
252  void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
253  CheckFailed(Message);
254  if (OS)
255  WriteTs(V1, Vs...);
256  }
257 
258  /// A debug info check failed.
259  void DebugInfoCheckFailed(const Twine &Message) {
260  if (OS)
261  *OS << Message << '\n';
263  BrokenDebugInfo = true;
264  }
265 
266  /// A debug info check failed (with values to print).
267  template <typename T1, typename... Ts>
268  void DebugInfoCheckFailed(const Twine &Message, const T1 &V1,
269  const Ts &... Vs) {
270  DebugInfoCheckFailed(Message);
271  if (OS)
272  WriteTs(V1, Vs...);
273  }
274 };
275 
276 } // namespace llvm
277 
278 namespace {
279 
280 class Verifier : public InstVisitor<Verifier>, VerifierSupport {
281  friend class InstVisitor<Verifier>;
282 
283  DominatorTree DT;
284 
285  /// When verifying a basic block, keep track of all of the
286  /// instructions we have seen so far.
287  ///
288  /// This allows us to do efficient dominance checks for the case when an
289  /// instruction has an operand that is an instruction in the same block.
290  SmallPtrSet<Instruction *, 16> InstsInThisBlock;
291 
292  /// Keep track of the metadata nodes that have been checked already.
294 
295  /// Keep track which DISubprogram is attached to which function.
296  DenseMap<const DISubprogram *, const Function *> DISubprogramAttachments;
297 
298  /// Track all DICompileUnits visited.
300 
301  /// The result type for a landingpad.
302  Type *LandingPadResultTy;
303 
304  /// Whether we've seen a call to @llvm.localescape in this function
305  /// already.
306  bool SawFrameEscape;
307 
308  /// Whether the current function has a DISubprogram attached to it.
309  bool HasDebugInfo = false;
310 
311  /// The current source language.
312  dwarf::SourceLanguage CurrentSourceLang = dwarf::DW_LANG_lo_user;
313 
314  /// Whether source was present on the first DIFile encountered in each CU.
315  DenseMap<const DICompileUnit *, bool> HasSourceDebugInfo;
316 
317  /// Stores the count of how many objects were passed to llvm.localescape for a
318  /// given function and the largest index passed to llvm.localrecover.
320 
321  // Maps catchswitches and cleanuppads that unwind to siblings to the
322  // terminators that indicate the unwind, used to detect cycles therein.
323  MapVector<Instruction *, Instruction *> SiblingFuncletInfo;
324 
325  /// Cache of constants visited in search of ConstantExprs.
326  SmallPtrSet<const Constant *, 32> ConstantExprVisited;
327 
328  /// Cache of declarations of the llvm.experimental.deoptimize.<ty> intrinsic.
329  SmallVector<const Function *, 4> DeoptimizeDeclarations;
330 
331  /// Cache of attribute lists verified.
332  SmallPtrSet<const void *, 32> AttributeListsVisited;
333 
334  // Verify that this GlobalValue is only used in this module.
335  // This map is used to avoid visiting uses twice. We can arrive at a user
336  // twice, if they have multiple operands. In particular for very large
337  // constant expressions, we can arrive at a particular user many times.
338  SmallPtrSet<const Value *, 32> GlobalValueVisited;
339 
340  // Keeps track of duplicate function argument debug info.
342 
343  TBAAVerifier TBAAVerifyHelper;
344 
345  SmallVector<IntrinsicInst *, 4> NoAliasScopeDecls;
346 
347  void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
348 
349 public:
350  explicit Verifier(raw_ostream *OS, bool ShouldTreatBrokenDebugInfoAsError,
351  const Module &M)
352  : VerifierSupport(OS, M), LandingPadResultTy(nullptr),
353  SawFrameEscape(false), TBAAVerifyHelper(this) {
354  TreatBrokenDebugInfoAsError = ShouldTreatBrokenDebugInfoAsError;
355  }
356 
357  bool hasBrokenDebugInfo() const { return BrokenDebugInfo; }
358 
359  bool verify(const Function &F) {
360  assert(F.getParent() == &M &&
361  "An instance of this class only works with a specific module!");
362 
363  // First ensure the function is well-enough formed to compute dominance
364  // information, and directly compute a dominance tree. We don't rely on the
365  // pass manager to provide this as it isolates us from a potentially
366  // out-of-date dominator tree and makes it significantly more complex to run
367  // this code outside of a pass manager.
368  // FIXME: It's really gross that we have to cast away constness here.
369  if (!F.empty())
370  DT.recalculate(const_cast<Function &>(F));
371 
372  for (const BasicBlock &BB : F) {
373  if (!BB.empty() && BB.back().isTerminator())
374  continue;
375 
376  if (OS) {
377  *OS << "Basic Block in function '" << F.getName()
378  << "' does not have terminator!\n";
379  BB.printAsOperand(*OS, true, MST);
380  *OS << "\n";
381  }
382  return false;
383  }
384 
385  Broken = false;
386  // FIXME: We strip const here because the inst visitor strips const.
387  visit(const_cast<Function &>(F));
388  verifySiblingFuncletUnwinds();
389  InstsInThisBlock.clear();
390  DebugFnArgs.clear();
391  LandingPadResultTy = nullptr;
392  SawFrameEscape = false;
393  SiblingFuncletInfo.clear();
394  verifyNoAliasScopeDecl();
395  NoAliasScopeDecls.clear();
396 
397  return !Broken;
398  }
399 
400  /// Verify the module that this instance of \c Verifier was initialized with.
401  bool verify() {
402  Broken = false;
403 
404  // Collect all declarations of the llvm.experimental.deoptimize intrinsic.
405  for (const Function &F : M)
406  if (F.getIntrinsicID() == Intrinsic::experimental_deoptimize)
407  DeoptimizeDeclarations.push_back(&F);
408 
409  // Now that we've visited every function, verify that we never asked to
410  // recover a frame index that wasn't escaped.
411  verifyFrameRecoverIndices();
412  for (const GlobalVariable &GV : M.globals())
413  visitGlobalVariable(GV);
414 
415  for (const GlobalAlias &GA : M.aliases())
416  visitGlobalAlias(GA);
417 
418  for (const NamedMDNode &NMD : M.named_metadata())
419  visitNamedMDNode(NMD);
420 
421  for (const StringMapEntry<Comdat> &SMEC : M.getComdatSymbolTable())
422  visitComdat(SMEC.getValue());
423 
424  visitModuleFlags(M);
425  visitModuleIdents(M);
426  visitModuleCommandLines(M);
427 
428  verifyCompileUnits();
429 
430  verifyDeoptimizeCallingConvs();
431  DISubprogramAttachments.clear();
432  return !Broken;
433  }
434 
435 private:
436  /// Whether a metadata node is allowed to be, or contain, a DILocation.
437  enum class AreDebugLocsAllowed { No, Yes };
438 
439  // Verification methods...
440  void visitGlobalValue(const GlobalValue &GV);
441  void visitGlobalVariable(const GlobalVariable &GV);
442  void visitGlobalAlias(const GlobalAlias &GA);
443  void visitAliaseeSubExpr(const GlobalAlias &A, const Constant &C);
444  void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
445  const GlobalAlias &A, const Constant &C);
446  void visitNamedMDNode(const NamedMDNode &NMD);
447  void visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs);
448  void visitMetadataAsValue(const MetadataAsValue &MD, Function *F);
449  void visitValueAsMetadata(const ValueAsMetadata &MD, Function *F);
450  void visitComdat(const Comdat &C);
451  void visitModuleIdents(const Module &M);
452  void visitModuleCommandLines(const Module &M);
453  void visitModuleFlags(const Module &M);
454  void visitModuleFlag(const MDNode *Op,
456  SmallVectorImpl<const MDNode *> &Requirements);
457  void visitModuleFlagCGProfileEntry(const MDOperand &MDO);
458  void visitFunction(const Function &F);
459  void visitBasicBlock(BasicBlock &BB);
460  void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
461  void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
462  void visitProfMetadata(Instruction &I, MDNode *MD);
463  void visitAnnotationMetadata(MDNode *Annotation);
464 
465  template <class Ty> bool isValidMetadataArray(const MDTuple &N);
466 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
467 #include "llvm/IR/Metadata.def"
468  void visitDIScope(const DIScope &N);
469  void visitDIVariable(const DIVariable &N);
470  void visitDILexicalBlockBase(const DILexicalBlockBase &N);
471  void visitDITemplateParameter(const DITemplateParameter &N);
472 
473  void visitTemplateParams(const MDNode &N, const Metadata &RawParams);
474 
475  // InstVisitor overrides...
477  void visit(Instruction &I);
478 
479  void visitTruncInst(TruncInst &I);
480  void visitZExtInst(ZExtInst &I);
481  void visitSExtInst(SExtInst &I);
482  void visitFPTruncInst(FPTruncInst &I);
483  void visitFPExtInst(FPExtInst &I);
484  void visitFPToUIInst(FPToUIInst &I);
485  void visitFPToSIInst(FPToSIInst &I);
486  void visitUIToFPInst(UIToFPInst &I);
487  void visitSIToFPInst(SIToFPInst &I);
488  void visitIntToPtrInst(IntToPtrInst &I);
489  void visitPtrToIntInst(PtrToIntInst &I);
490  void visitBitCastInst(BitCastInst &I);
491  void visitAddrSpaceCastInst(AddrSpaceCastInst &I);
492  void visitPHINode(PHINode &PN);
493  void visitCallBase(CallBase &Call);
494  void visitUnaryOperator(UnaryOperator &U);
495  void visitBinaryOperator(BinaryOperator &B);
496  void visitICmpInst(ICmpInst &IC);
497  void visitFCmpInst(FCmpInst &FC);
498  void visitExtractElementInst(ExtractElementInst &EI);
499  void visitInsertElementInst(InsertElementInst &EI);
500  void visitShuffleVectorInst(ShuffleVectorInst &EI);
501  void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
502  void visitCallInst(CallInst &CI);
503  void visitInvokeInst(InvokeInst &II);
504  void visitGetElementPtrInst(GetElementPtrInst &GEP);
505  void visitLoadInst(LoadInst &LI);
506  void visitStoreInst(StoreInst &SI);
507  void verifyDominatesUse(Instruction &I, unsigned i);
508  void visitInstruction(Instruction &I);
509  void visitTerminator(Instruction &I);
510  void visitBranchInst(BranchInst &BI);
511  void visitReturnInst(ReturnInst &RI);
512  void visitSwitchInst(SwitchInst &SI);
513  void visitIndirectBrInst(IndirectBrInst &BI);
514  void visitCallBrInst(CallBrInst &CBI);
515  void visitSelectInst(SelectInst &SI);
516  void visitUserOp1(Instruction &I);
517  void visitUserOp2(Instruction &I) { visitUserOp1(I); }
518  void visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call);
519  void visitConstrainedFPIntrinsic(ConstrainedFPIntrinsic &FPI);
520  void visitDbgIntrinsic(StringRef Kind, DbgVariableIntrinsic &DII);
521  void visitDbgLabelIntrinsic(StringRef Kind, DbgLabelInst &DLI);
522  void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
523  void visitAtomicRMWInst(AtomicRMWInst &RMWI);
524  void visitFenceInst(FenceInst &FI);
525  void visitAllocaInst(AllocaInst &AI);
526  void visitExtractValueInst(ExtractValueInst &EVI);
527  void visitInsertValueInst(InsertValueInst &IVI);
528  void visitEHPadPredecessors(Instruction &I);
529  void visitLandingPadInst(LandingPadInst &LPI);
530  void visitResumeInst(ResumeInst &RI);
531  void visitCatchPadInst(CatchPadInst &CPI);
532  void visitCatchReturnInst(CatchReturnInst &CatchReturn);
533  void visitCleanupPadInst(CleanupPadInst &CPI);
534  void visitFuncletPadInst(FuncletPadInst &FPI);
535  void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
536  void visitCleanupReturnInst(CleanupReturnInst &CRI);
537 
538  void verifySwiftErrorCall(CallBase &Call, const Value *SwiftErrorVal);
539  void verifySwiftErrorValue(const Value *SwiftErrorVal);
540  void verifyTailCCMustTailAttrs(AttrBuilder Attrs, StringRef Context);
541  void verifyMustTailCall(CallInst &CI);
542  bool verifyAttributeCount(AttributeList Attrs, unsigned Params);
543  void verifyAttributeTypes(AttributeSet Attrs, const Value *V);
544  void verifyParameterAttrs(AttributeSet Attrs, Type *Ty, const Value *V);
545  void checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
546  const Value *V);
547  void verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
548  const Value *V, bool IsIntrinsic);
549  void verifyFunctionMetadata(ArrayRef<std::pair<unsigned, MDNode *>> MDs);
550 
551  void visitConstantExprsRecursively(const Constant *EntryC);
552  void visitConstantExpr(const ConstantExpr *CE);
553  void verifyStatepoint(const CallBase &Call);
554  void verifyFrameRecoverIndices();
555  void verifySiblingFuncletUnwinds();
556 
557  void verifyFragmentExpression(const DbgVariableIntrinsic &I);
558  template <typename ValueOrMetadata>
559  void verifyFragmentExpression(const DIVariable &V,
561  ValueOrMetadata *Desc);
562  void verifyFnArgs(const DbgVariableIntrinsic &I);
563  void verifyNotEntryValue(const DbgVariableIntrinsic &I);
564 
565  /// Module-level debug info verification...
566  void verifyCompileUnits();
567 
568  /// Module-level verification that all @llvm.experimental.deoptimize
569  /// declarations share the same calling convention.
570  void verifyDeoptimizeCallingConvs();
571 
572  /// Verify all-or-nothing property of DIFile source attribute within a CU.
573  void verifySourceDebugInfo(const DICompileUnit &U, const DIFile &F);
574 
575  /// Verify the llvm.experimental.noalias.scope.decl declarations
576  void verifyNoAliasScopeDecl();
577 };
578 
579 } // end anonymous namespace
580 
581 /// We know that cond should be true, if not print an error message.
582 #define Assert(C, ...) \
583  do { if (!(C)) { CheckFailed(__VA_ARGS__); return; } } while (false)
584 
585 /// We know that a debug info condition should be true, if not print
586 /// an error message.
587 #define AssertDI(C, ...) \
588  do { if (!(C)) { DebugInfoCheckFailed(__VA_ARGS__); return; } } while (false)
589 
590 void Verifier::visit(Instruction &I) {
591  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
592  Assert(I.getOperand(i) != nullptr, "Operand is null", &I);
594 }
595 
596 // Helper to recursively iterate over indirect users. By
597 // returning false, the callback can ask to stop recursing
598 // further.
599 static void forEachUser(const Value *User,
601  llvm::function_ref<bool(const Value *)> Callback) {
602  if (!Visited.insert(User).second)
603  return;
604  for (const Value *TheNextUser : User->materialized_users())
605  if (Callback(TheNextUser))
606  forEachUser(TheNextUser, Visited, Callback);
607 }
608 
609 void Verifier::visitGlobalValue(const GlobalValue &GV) {
611  "Global is external, but doesn't have external or weak linkage!", &GV);
612 
613  if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV))
614  Assert(GO->getAlignment() <= Value::MaximumAlignment,
615  "huge alignment values are unsupported", GO);
616  Assert(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
617  "Only global variables can have appending linkage!", &GV);
618 
619  if (GV.hasAppendingLinkage()) {
620  const GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
621  Assert(GVar && GVar->getValueType()->isArrayTy(),
622  "Only global arrays can have appending linkage!", GVar);
623  }
624 
625  if (GV.isDeclarationForLinker())
626  Assert(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
627 
628  if (GV.hasDLLImportStorageClass()) {
629  Assert(!GV.isDSOLocal(),
630  "GlobalValue with DLLImport Storage is dso_local!", &GV);
631 
632  Assert((GV.isDeclaration() &&
633  (GV.hasExternalLinkage() || GV.hasExternalWeakLinkage())) ||
635  "Global is marked as dllimport, but not external", &GV);
636  }
637 
638  if (GV.isImplicitDSOLocal())
639  Assert(GV.isDSOLocal(),
640  "GlobalValue with local linkage or non-default "
641  "visibility must be dso_local!",
642  &GV);
643 
644  forEachUser(&GV, GlobalValueVisited, [&](const Value *V) -> bool {
645  if (const Instruction *I = dyn_cast<Instruction>(V)) {
646  if (!I->getParent() || !I->getParent()->getParent())
647  CheckFailed("Global is referenced by parentless instruction!", &GV, &M,
648  I);
649  else if (I->getParent()->getParent()->getParent() != &M)
650  CheckFailed("Global is referenced in a different module!", &GV, &M, I,
651  I->getParent()->getParent(),
652  I->getParent()->getParent()->getParent());
653  return false;
654  } else if (const Function *F = dyn_cast<Function>(V)) {
655  if (F->getParent() != &M)
656  CheckFailed("Global is used by function in a different module", &GV, &M,
657  F, F->getParent());
658  return false;
659  }
660  return true;
661  });
662 }
663 
664 void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
665  if (GV.hasInitializer()) {
666  Assert(GV.getInitializer()->getType() == GV.getValueType(),
667  "Global variable initializer type does not match global "
668  "variable type!",
669  &GV);
670  // If the global has common linkage, it must have a zero initializer and
671  // cannot be constant.
672  if (GV.hasCommonLinkage()) {
674  "'common' global must have a zero initializer!", &GV);
675  Assert(!GV.isConstant(), "'common' global may not be marked constant!",
676  &GV);
677  Assert(!GV.hasComdat(), "'common' global may not be in a Comdat!", &GV);
678  }
679  }
680 
681  if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
682  GV.getName() == "llvm.global_dtors")) {
684  "invalid linkage for intrinsic global variable", &GV);
685  // Don't worry about emitting an error for it not being an array,
686  // visitGlobalValue will complain on appending non-array.
687  if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getValueType())) {
688  StructType *STy = dyn_cast<StructType>(ATy->getElementType());
689  PointerType *FuncPtrTy =
691  getPointerTo(DL.getProgramAddressSpace());
692  Assert(STy &&
693  (STy->getNumElements() == 2 || STy->getNumElements() == 3) &&
694  STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
695  STy->getTypeAtIndex(1) == FuncPtrTy,
696  "wrong type for intrinsic global variable", &GV);
697  Assert(STy->getNumElements() == 3,
698  "the third field of the element type is mandatory, "
699  "specify i8* null to migrate from the obsoleted 2-field form");
700  Type *ETy = STy->getTypeAtIndex(2);
701  Type *Int8Ty = Type::getInt8Ty(ETy->getContext());
702  Assert(ETy->isPointerTy() &&
703  cast<PointerType>(ETy)->isOpaqueOrPointeeTypeMatches(Int8Ty),
704  "wrong type for intrinsic global variable", &GV);
705  }
706  }
707 
708  if (GV.hasName() && (GV.getName() == "llvm.used" ||
709  GV.getName() == "llvm.compiler.used")) {
711  "invalid linkage for intrinsic global variable", &GV);
712  Type *GVType = GV.getValueType();
713  if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
714  PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
715  Assert(PTy, "wrong type for intrinsic global variable", &GV);
716  if (GV.hasInitializer()) {
717  const Constant *Init = GV.getInitializer();
718  const ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
719  Assert(InitArray, "wrong initalizer for intrinsic global variable",
720  Init);
721  for (Value *Op : InitArray->operands()) {
722  Value *V = Op->stripPointerCasts();
723  Assert(isa<GlobalVariable>(V) || isa<Function>(V) ||
724  isa<GlobalAlias>(V),
725  "invalid llvm.used member", V);
726  Assert(V->hasName(), "members of llvm.used must be named", V);
727  }
728  }
729  }
730  }
731 
732  // Visit any debug info attachments.
734  GV.getMetadata(LLVMContext::MD_dbg, MDs);
735  for (auto *MD : MDs) {
736  if (auto *GVE = dyn_cast<DIGlobalVariableExpression>(MD))
737  visitDIGlobalVariableExpression(*GVE);
738  else
739  AssertDI(false, "!dbg attachment of global variable must be a "
740  "DIGlobalVariableExpression");
741  }
742 
743  // Scalable vectors cannot be global variables, since we don't know
744  // the runtime size. If the global is an array containing scalable vectors,
745  // that will be caught by the isValidElementType methods in StructType or
746  // ArrayType instead.
747  Assert(!isa<ScalableVectorType>(GV.getValueType()),
748  "Globals cannot contain scalable vectors", &GV);
749 
750  if (auto *STy = dyn_cast<StructType>(GV.getValueType()))
752  "Globals cannot contain scalable vectors", &GV);
753 
754  if (!GV.hasInitializer()) {
755  visitGlobalValue(GV);
756  return;
757  }
758 
759  // Walk any aggregate initializers looking for bitcasts between address spaces
760  visitConstantExprsRecursively(GV.getInitializer());
761 
762  visitGlobalValue(GV);
763 }
764 
765 void Verifier::visitAliaseeSubExpr(const GlobalAlias &GA, const Constant &C) {
767  Visited.insert(&GA);
768  visitAliaseeSubExpr(Visited, GA, C);
769 }
770 
771 void Verifier::visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias*> &Visited,
772  const GlobalAlias &GA, const Constant &C) {
773  if (const auto *GV = dyn_cast<GlobalValue>(&C)) {
774  Assert(!GV->isDeclarationForLinker(), "Alias must point to a definition",
775  &GA);
776 
777  if (const auto *GA2 = dyn_cast<GlobalAlias>(GV)) {
778  Assert(Visited.insert(GA2).second, "Aliases cannot form a cycle", &GA);
779 
780  Assert(!GA2->isInterposable(), "Alias cannot point to an interposable alias",
781  &GA);
782  } else {
783  // Only continue verifying subexpressions of GlobalAliases.
784  // Do not recurse into global initializers.
785  return;
786  }
787  }
788 
789  if (const auto *CE = dyn_cast<ConstantExpr>(&C))
790  visitConstantExprsRecursively(CE);
791 
792  for (const Use &U : C.operands()) {
793  Value *V = &*U;
794  if (const auto *GA2 = dyn_cast<GlobalAlias>(V))
795  visitAliaseeSubExpr(Visited, GA, *GA2->getAliasee());
796  else if (const auto *C2 = dyn_cast<Constant>(V))
797  visitAliaseeSubExpr(Visited, GA, *C2);
798  }
799 }
800 
801 void Verifier::visitGlobalAlias(const GlobalAlias &GA) {
803  "Alias should have private, internal, linkonce, weak, linkonce_odr, "
804  "weak_odr, or external linkage!",
805  &GA);
806  const Constant *Aliasee = GA.getAliasee();
807  Assert(Aliasee, "Aliasee cannot be NULL!", &GA);
808  Assert(GA.getType() == Aliasee->getType(),
809  "Alias and aliasee types should match!", &GA);
810 
811  Assert(isa<GlobalValue>(Aliasee) || isa<ConstantExpr>(Aliasee),
812  "Aliasee should be either GlobalValue or ConstantExpr", &GA);
813 
814  visitAliaseeSubExpr(GA, *Aliasee);
815 
816  visitGlobalValue(GA);
817 }
818 
819 void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
820  // There used to be various other llvm.dbg.* nodes, but we don't support
821  // upgrading them and we want to reserve the namespace for future uses.
822  if (NMD.getName().startswith("llvm.dbg."))
823  AssertDI(NMD.getName() == "llvm.dbg.cu",
824  "unrecognized named metadata node in the llvm.dbg namespace",
825  &NMD);
826  for (const MDNode *MD : NMD.operands()) {
827  if (NMD.getName() == "llvm.dbg.cu")
828  AssertDI(MD && isa<DICompileUnit>(MD), "invalid compile unit", &NMD, MD);
829 
830  if (!MD)
831  continue;
832 
833  visitMDNode(*MD, AreDebugLocsAllowed::Yes);
834  }
835 }
836 
837 void Verifier::visitMDNode(const MDNode &MD, AreDebugLocsAllowed AllowLocs) {
838  // Only visit each node once. Metadata can be mutually recursive, so this
839  // avoids infinite recursion here, as well as being an optimization.
840  if (!MDNodes.insert(&MD).second)
841  return;
842 
843  Assert(&MD.getContext() == &Context,
844  "MDNode context does not match Module context!", &MD);
845 
846  switch (MD.getMetadataID()) {
847  default:
848  llvm_unreachable("Invalid MDNode subclass");
849  case Metadata::MDTupleKind:
850  break;
851 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
852  case Metadata::CLASS##Kind: \
853  visit##CLASS(cast<CLASS>(MD)); \
854  break;
855 #include "llvm/IR/Metadata.def"
856  }
857 
858  for (const Metadata *Op : MD.operands()) {
859  if (!Op)
860  continue;
861  Assert(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
862  &MD, Op);
863  AssertDI(!isa<DILocation>(Op) || AllowLocs == AreDebugLocsAllowed::Yes,
864  "DILocation not allowed within this metadata node", &MD, Op);
865  if (auto *N = dyn_cast<MDNode>(Op)) {
866  visitMDNode(*N, AllowLocs);
867  continue;
868  }
869  if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
870  visitValueAsMetadata(*V, nullptr);
871  continue;
872  }
873  }
874 
875  // Check these last, so we diagnose problems in operands first.
876  Assert(!MD.isTemporary(), "Expected no forward declarations!", &MD);
877  Assert(MD.isResolved(), "All nodes should be resolved!", &MD);
878 }
879 
880 void Verifier::visitValueAsMetadata(const ValueAsMetadata &MD, Function *F) {
881  Assert(MD.getValue(), "Expected valid value", &MD);
882  Assert(!MD.getValue()->getType()->isMetadataTy(),
883  "Unexpected metadata round-trip through values", &MD, MD.getValue());
884 
885  auto *L = dyn_cast<LocalAsMetadata>(&MD);
886  if (!L)
887  return;
888 
889  Assert(F, "function-local metadata used outside a function", L);
890 
891  // If this was an instruction, bb, or argument, verify that it is in the
892  // function that we expect.
893  Function *ActualF = nullptr;
894  if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
895  Assert(I->getParent(), "function-local metadata not in basic block", L, I);
896  ActualF = I->getParent()->getParent();
897  } else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
898  ActualF = BB->getParent();
899  else if (Argument *A = dyn_cast<Argument>(L->getValue()))
900  ActualF = A->getParent();
901  assert(ActualF && "Unimplemented function local metadata case!");
902 
903  Assert(ActualF == F, "function-local metadata used in wrong function", L);
904 }
905 
906 void Verifier::visitMetadataAsValue(const MetadataAsValue &MDV, Function *F) {
907  Metadata *MD = MDV.getMetadata();
908  if (auto *N = dyn_cast<MDNode>(MD)) {
909  visitMDNode(*N, AreDebugLocsAllowed::No);
910  return;
911  }
912 
913  // Only visit each node once. Metadata can be mutually recursive, so this
914  // avoids infinite recursion here, as well as being an optimization.
915  if (!MDNodes.insert(MD).second)
916  return;
917 
918  if (auto *V = dyn_cast<ValueAsMetadata>(MD))
919  visitValueAsMetadata(*V, F);
920 }
921 
922 static bool isType(const Metadata *MD) { return !MD || isa<DIType>(MD); }
923 static bool isScope(const Metadata *MD) { return !MD || isa<DIScope>(MD); }
924 static bool isDINode(const Metadata *MD) { return !MD || isa<DINode>(MD); }
925 
926 void Verifier::visitDILocation(const DILocation &N) {
927  AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
928  "location requires a valid scope", &N, N.getRawScope());
929  if (auto *IA = N.getRawInlinedAt())
930  AssertDI(isa<DILocation>(IA), "inlined-at should be a location", &N, IA);
931  if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
932  AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
933 }
934 
935 void Verifier::visitGenericDINode(const GenericDINode &N) {
936  AssertDI(N.getTag(), "invalid tag", &N);
937 }
938 
939 void Verifier::visitDIScope(const DIScope &N) {
940  if (auto *F = N.getRawFile())
941  AssertDI(isa<DIFile>(F), "invalid file", &N, F);
942 }
943 
944 void Verifier::visitDISubrange(const DISubrange &N) {
945  AssertDI(N.getTag() == dwarf::DW_TAG_subrange_type, "invalid tag", &N);
946  bool HasAssumedSizedArraySupport = dwarf::isFortran(CurrentSourceLang);
947  AssertDI(HasAssumedSizedArraySupport || N.getRawCountNode() ||
948  N.getRawUpperBound(),
949  "Subrange must contain count or upperBound", &N);
950  AssertDI(!N.getRawCountNode() || !N.getRawUpperBound(),
951  "Subrange can have any one of count or upperBound", &N);
952  auto *CBound = N.getRawCountNode();
953  AssertDI(!CBound || isa<ConstantAsMetadata>(CBound) ||
954  isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
955  "Count must be signed constant or DIVariable or DIExpression", &N);
956  auto Count = N.getCount();
957  AssertDI(!Count || !Count.is<ConstantInt *>() ||
958  Count.get<ConstantInt *>()->getSExtValue() >= -1,
959  "invalid subrange count", &N);
960  auto *LBound = N.getRawLowerBound();
961  AssertDI(!LBound || isa<ConstantAsMetadata>(LBound) ||
962  isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
963  "LowerBound must be signed constant or DIVariable or DIExpression",
964  &N);
965  auto *UBound = N.getRawUpperBound();
966  AssertDI(!UBound || isa<ConstantAsMetadata>(UBound) ||
967  isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
968  "UpperBound must be signed constant or DIVariable or DIExpression",
969  &N);
970  auto *Stride = N.getRawStride();
971  AssertDI(!Stride || isa<ConstantAsMetadata>(Stride) ||
972  isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
973  "Stride must be signed constant or DIVariable or DIExpression", &N);
974 }
975 
976 void Verifier::visitDIGenericSubrange(const DIGenericSubrange &N) {
977  AssertDI(N.getTag() == dwarf::DW_TAG_generic_subrange, "invalid tag", &N);
978  AssertDI(N.getRawCountNode() || N.getRawUpperBound(),
979  "GenericSubrange must contain count or upperBound", &N);
980  AssertDI(!N.getRawCountNode() || !N.getRawUpperBound(),
981  "GenericSubrange can have any one of count or upperBound", &N);
982  auto *CBound = N.getRawCountNode();
983  AssertDI(!CBound || isa<DIVariable>(CBound) || isa<DIExpression>(CBound),
984  "Count must be signed constant or DIVariable or DIExpression", &N);
985  auto *LBound = N.getRawLowerBound();
986  AssertDI(LBound, "GenericSubrange must contain lowerBound", &N);
987  AssertDI(isa<DIVariable>(LBound) || isa<DIExpression>(LBound),
988  "LowerBound must be signed constant or DIVariable or DIExpression",
989  &N);
990  auto *UBound = N.getRawUpperBound();
991  AssertDI(!UBound || isa<DIVariable>(UBound) || isa<DIExpression>(UBound),
992  "UpperBound must be signed constant or DIVariable or DIExpression",
993  &N);
994  auto *Stride = N.getRawStride();
995  AssertDI(Stride, "GenericSubrange must contain stride", &N);
996  AssertDI(isa<DIVariable>(Stride) || isa<DIExpression>(Stride),
997  "Stride must be signed constant or DIVariable or DIExpression", &N);
998 }
999 
1000 void Verifier::visitDIEnumerator(const DIEnumerator &N) {
1001  AssertDI(N.getTag() == dwarf::DW_TAG_enumerator, "invalid tag", &N);
1002 }
1003 
1004 void Verifier::visitDIBasicType(const DIBasicType &N) {
1005  AssertDI(N.getTag() == dwarf::DW_TAG_base_type ||
1006  N.getTag() == dwarf::DW_TAG_unspecified_type ||
1007  N.getTag() == dwarf::DW_TAG_string_type,
1008  "invalid tag", &N);
1009 }
1010 
1011 void Verifier::visitDIStringType(const DIStringType &N) {
1012  AssertDI(N.getTag() == dwarf::DW_TAG_string_type, "invalid tag", &N);
1013  AssertDI(!(N.isBigEndian() && N.isLittleEndian()) ,
1014  "has conflicting flags", &N);
1015 }
1016 
1017 void Verifier::visitDIDerivedType(const DIDerivedType &N) {
1018  // Common scope checks.
1019  visitDIScope(N);
1020 
1021  AssertDI(N.getTag() == dwarf::DW_TAG_typedef ||
1022  N.getTag() == dwarf::DW_TAG_pointer_type ||
1023  N.getTag() == dwarf::DW_TAG_ptr_to_member_type ||
1024  N.getTag() == dwarf::DW_TAG_reference_type ||
1025  N.getTag() == dwarf::DW_TAG_rvalue_reference_type ||
1026  N.getTag() == dwarf::DW_TAG_const_type ||
1027  N.getTag() == dwarf::DW_TAG_volatile_type ||
1028  N.getTag() == dwarf::DW_TAG_restrict_type ||
1029  N.getTag() == dwarf::DW_TAG_atomic_type ||
1030  N.getTag() == dwarf::DW_TAG_member ||
1031  N.getTag() == dwarf::DW_TAG_inheritance ||
1032  N.getTag() == dwarf::DW_TAG_friend ||
1033  N.getTag() == dwarf::DW_TAG_set_type,
1034  "invalid tag", &N);
1035  if (N.getTag() == dwarf::DW_TAG_ptr_to_member_type) {
1036  AssertDI(isType(N.getRawExtraData()), "invalid pointer to member type", &N,
1037  N.getRawExtraData());
1038  }
1039 
1040  if (N.getTag() == dwarf::DW_TAG_set_type) {
1041  if (auto *T = N.getRawBaseType()) {
1042  auto *Enum = dyn_cast_or_null<DICompositeType>(T);
1043  auto *Basic = dyn_cast_or_null<DIBasicType>(T);
1044  AssertDI(
1045  (Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type) ||
1046  (Basic && (Basic->getEncoding() == dwarf::DW_ATE_unsigned ||
1047  Basic->getEncoding() == dwarf::DW_ATE_signed ||
1048  Basic->getEncoding() == dwarf::DW_ATE_unsigned_char ||
1049  Basic->getEncoding() == dwarf::DW_ATE_signed_char ||
1050  Basic->getEncoding() == dwarf::DW_ATE_boolean)),
1051  "invalid set base type", &N, T);
1052  }
1053  }
1054 
1055  AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1056  AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
1057  N.getRawBaseType());
1058 
1059  if (N.getDWARFAddressSpace()) {
1060  AssertDI(N.getTag() == dwarf::DW_TAG_pointer_type ||
1061  N.getTag() == dwarf::DW_TAG_reference_type ||
1062  N.getTag() == dwarf::DW_TAG_rvalue_reference_type,
1063  "DWARF address space only applies to pointer or reference types",
1064  &N);
1065  }
1066 }
1067 
1068 /// Detect mutually exclusive flags.
1069 static bool hasConflictingReferenceFlags(unsigned Flags) {
1070  return ((Flags & DINode::FlagLValueReference) &&
1071  (Flags & DINode::FlagRValueReference)) ||
1072  ((Flags & DINode::FlagTypePassByValue) &&
1073  (Flags & DINode::FlagTypePassByReference));
1074 }
1075 
1076 void Verifier::visitTemplateParams(const MDNode &N, const Metadata &RawParams) {
1077  auto *Params = dyn_cast<MDTuple>(&RawParams);
1078  AssertDI(Params, "invalid template params", &N, &RawParams);
1079  for (Metadata *Op : Params->operands()) {
1080  AssertDI(Op && isa<DITemplateParameter>(Op), "invalid template parameter",
1081  &N, Params, Op);
1082  }
1083 }
1084 
1085 void Verifier::visitDICompositeType(const DICompositeType &N) {
1086  // Common scope checks.
1087  visitDIScope(N);
1088 
1089  AssertDI(N.getTag() == dwarf::DW_TAG_array_type ||
1090  N.getTag() == dwarf::DW_TAG_structure_type ||
1091  N.getTag() == dwarf::DW_TAG_union_type ||
1092  N.getTag() == dwarf::DW_TAG_enumeration_type ||
1093  N.getTag() == dwarf::DW_TAG_class_type ||
1094  N.getTag() == dwarf::DW_TAG_variant_part,
1095  "invalid tag", &N);
1096 
1097  AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1098  AssertDI(isType(N.getRawBaseType()), "invalid base type", &N,
1099  N.getRawBaseType());
1100 
1101  AssertDI(!N.getRawElements() || isa<MDTuple>(N.getRawElements()),
1102  "invalid composite elements", &N, N.getRawElements());
1103  AssertDI(isType(N.getRawVTableHolder()), "invalid vtable holder", &N,
1104  N.getRawVTableHolder());
1106  "invalid reference flags", &N);
1107  unsigned DIBlockByRefStruct = 1 << 4;
1108  AssertDI((N.getFlags() & DIBlockByRefStruct) == 0,
1109  "DIBlockByRefStruct on DICompositeType is no longer supported", &N);
1110 
1111  if (N.isVector()) {
1112  const DINodeArray Elements = N.getElements();
1113  AssertDI(Elements.size() == 1 &&
1114  Elements[0]->getTag() == dwarf::DW_TAG_subrange_type,
1115  "invalid vector, expected one element of type subrange", &N);
1116  }
1117 
1118  if (auto *Params = N.getRawTemplateParams())
1119  visitTemplateParams(N, *Params);
1120 
1121  if (auto *D = N.getRawDiscriminator()) {
1122  AssertDI(isa<DIDerivedType>(D) && N.getTag() == dwarf::DW_TAG_variant_part,
1123  "discriminator can only appear on variant part");
1124  }
1125 
1126  if (N.getRawDataLocation()) {
1127  AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1128  "dataLocation can only appear in array type");
1129  }
1130 
1131  if (N.getRawAssociated()) {
1132  AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1133  "associated can only appear in array type");
1134  }
1135 
1136  if (N.getRawAllocated()) {
1137  AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1138  "allocated can only appear in array type");
1139  }
1140 
1141  if (N.getRawRank()) {
1142  AssertDI(N.getTag() == dwarf::DW_TAG_array_type,
1143  "rank can only appear in array type");
1144  }
1145 }
1146 
1147 void Verifier::visitDISubroutineType(const DISubroutineType &N) {
1148  AssertDI(N.getTag() == dwarf::DW_TAG_subroutine_type, "invalid tag", &N);
1149  if (auto *Types = N.getRawTypeArray()) {
1150  AssertDI(isa<MDTuple>(Types), "invalid composite elements", &N, Types);
1151  for (Metadata *Ty : N.getTypeArray()->operands()) {
1152  AssertDI(isType(Ty), "invalid subroutine type ref", &N, Types, Ty);
1153  }
1154  }
1156  "invalid reference flags", &N);
1157 }
1158 
1159 void Verifier::visitDIFile(const DIFile &N) {
1160  AssertDI(N.getTag() == dwarf::DW_TAG_file_type, "invalid tag", &N);
1161  Optional<DIFile::ChecksumInfo<StringRef>> Checksum = N.getChecksum();
1162  if (Checksum) {
1163  AssertDI(Checksum->Kind <= DIFile::ChecksumKind::CSK_Last,
1164  "invalid checksum kind", &N);
1165  size_t Size;
1166  switch (Checksum->Kind) {
1167  case DIFile::CSK_MD5:
1168  Size = 32;
1169  break;
1170  case DIFile::CSK_SHA1:
1171  Size = 40;
1172  break;
1173  case DIFile::CSK_SHA256:
1174  Size = 64;
1175  break;
1176  }
1177  AssertDI(Checksum->Value.size() == Size, "invalid checksum length", &N);
1178  AssertDI(Checksum->Value.find_if_not(llvm::isHexDigit) == StringRef::npos,
1179  "invalid checksum", &N);
1180  }
1181 }
1182 
1183 void Verifier::visitDICompileUnit(const DICompileUnit &N) {
1184  AssertDI(N.isDistinct(), "compile units must be distinct", &N);
1185  AssertDI(N.getTag() == dwarf::DW_TAG_compile_unit, "invalid tag", &N);
1186 
1187  // Don't bother verifying the compilation directory or producer string
1188  // as those could be empty.
1189  AssertDI(N.getRawFile() && isa<DIFile>(N.getRawFile()), "invalid file", &N,
1190  N.getRawFile());
1191  AssertDI(!N.getFile()->getFilename().empty(), "invalid filename", &N,
1192  N.getFile());
1193 
1194  CurrentSourceLang = (dwarf::SourceLanguage)N.getSourceLanguage();
1195 
1196  verifySourceDebugInfo(N, *N.getFile());
1197 
1198  AssertDI((N.getEmissionKind() <= DICompileUnit::LastEmissionKind),
1199  "invalid emission kind", &N);
1200 
1201  if (auto *Array = N.getRawEnumTypes()) {
1202  AssertDI(isa<MDTuple>(Array), "invalid enum list", &N, Array);
1203  for (Metadata *Op : N.getEnumTypes()->operands()) {
1204  auto *Enum = dyn_cast_or_null<DICompositeType>(Op);
1205  AssertDI(Enum && Enum->getTag() == dwarf::DW_TAG_enumeration_type,
1206  "invalid enum type", &N, N.getEnumTypes(), Op);
1207  }
1208  }
1209  if (auto *Array = N.getRawRetainedTypes()) {
1210  AssertDI(isa<MDTuple>(Array), "invalid retained type list", &N, Array);
1211  for (Metadata *Op : N.getRetainedTypes()->operands()) {
1212  AssertDI(Op && (isa<DIType>(Op) ||
1213  (isa<DISubprogram>(Op) &&
1214  !cast<DISubprogram>(Op)->isDefinition())),
1215  "invalid retained type", &N, Op);
1216  }
1217  }
1218  if (auto *Array = N.getRawGlobalVariables()) {
1219  AssertDI(isa<MDTuple>(Array), "invalid global variable list", &N, Array);
1220  for (Metadata *Op : N.getGlobalVariables()->operands()) {
1221  AssertDI(Op && (isa<DIGlobalVariableExpression>(Op)),
1222  "invalid global variable ref", &N, Op);
1223  }
1224  }
1225  if (auto *Array = N.getRawImportedEntities()) {
1226  AssertDI(isa<MDTuple>(Array), "invalid imported entity list", &N, Array);
1227  for (Metadata *Op : N.getImportedEntities()->operands()) {
1228  AssertDI(Op && isa<DIImportedEntity>(Op), "invalid imported entity ref",
1229  &N, Op);
1230  }
1231  }
1232  if (auto *Array = N.getRawMacros()) {
1233  AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1234  for (Metadata *Op : N.getMacros()->operands()) {
1235  AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1236  }
1237  }
1238  CUVisited.insert(&N);
1239 }
1240 
1241 void Verifier::visitDISubprogram(const DISubprogram &N) {
1242  AssertDI(N.getTag() == dwarf::DW_TAG_subprogram, "invalid tag", &N);
1243  AssertDI(isScope(N.getRawScope()), "invalid scope", &N, N.getRawScope());
1244  if (auto *F = N.getRawFile())
1245  AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1246  else
1247  AssertDI(N.getLine() == 0, "line specified with no file", &N, N.getLine());
1248  if (auto *T = N.getRawType())
1249  AssertDI(isa<DISubroutineType>(T), "invalid subroutine type", &N, T);
1250  AssertDI(isType(N.getRawContainingType()), "invalid containing type", &N,
1251  N.getRawContainingType());
1252  if (auto *Params = N.getRawTemplateParams())
1253  visitTemplateParams(N, *Params);
1254  if (auto *S = N.getRawDeclaration())
1255  AssertDI(isa<DISubprogram>(S) && !cast<DISubprogram>(S)->isDefinition(),
1256  "invalid subprogram declaration", &N, S);
1257  if (auto *RawNode = N.getRawRetainedNodes()) {
1258  auto *Node = dyn_cast<MDTuple>(RawNode);
1259  AssertDI(Node, "invalid retained nodes list", &N, RawNode);
1260  for (Metadata *Op : Node->operands()) {
1261  AssertDI(Op && (isa<DILocalVariable>(Op) || isa<DILabel>(Op)),
1262  "invalid retained nodes, expected DILocalVariable or DILabel",
1263  &N, Node, Op);
1264  }
1265  }
1267  "invalid reference flags", &N);
1268 
1269  auto *Unit = N.getRawUnit();
1270  if (N.isDefinition()) {
1271  // Subprogram definitions (not part of the type hierarchy).
1272  AssertDI(N.isDistinct(), "subprogram definitions must be distinct", &N);
1273  AssertDI(Unit, "subprogram definitions must have a compile unit", &N);
1274  AssertDI(isa<DICompileUnit>(Unit), "invalid unit type", &N, Unit);
1275  if (N.getFile())
1276  verifySourceDebugInfo(*N.getUnit(), *N.getFile());
1277  } else {
1278  // Subprogram declarations (part of the type hierarchy).
1279  AssertDI(!Unit, "subprogram declarations must not have a compile unit", &N);
1280  }
1281 
1282  if (auto *RawThrownTypes = N.getRawThrownTypes()) {
1283  auto *ThrownTypes = dyn_cast<MDTuple>(RawThrownTypes);
1284  AssertDI(ThrownTypes, "invalid thrown types list", &N, RawThrownTypes);
1285  for (Metadata *Op : ThrownTypes->operands())
1286  AssertDI(Op && isa<DIType>(Op), "invalid thrown type", &N, ThrownTypes,
1287  Op);
1288  }
1289 
1290  if (N.areAllCallsDescribed())
1291  AssertDI(N.isDefinition(),
1292  "DIFlagAllCallsDescribed must be attached to a definition");
1293 }
1294 
1295 void Verifier::visitDILexicalBlockBase(const DILexicalBlockBase &N) {
1296  AssertDI(N.getTag() == dwarf::DW_TAG_lexical_block, "invalid tag", &N);
1297  AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1298  "invalid local scope", &N, N.getRawScope());
1299  if (auto *SP = dyn_cast<DISubprogram>(N.getRawScope()))
1300  AssertDI(SP->isDefinition(), "scope points into the type hierarchy", &N);
1301 }
1302 
1303 void Verifier::visitDILexicalBlock(const DILexicalBlock &N) {
1304  visitDILexicalBlockBase(N);
1305 
1306  AssertDI(N.getLine() || !N.getColumn(),
1307  "cannot have column info without line info", &N);
1308 }
1309 
1310 void Verifier::visitDILexicalBlockFile(const DILexicalBlockFile &N) {
1311  visitDILexicalBlockBase(N);
1312 }
1313 
1314 void Verifier::visitDICommonBlock(const DICommonBlock &N) {
1315  AssertDI(N.getTag() == dwarf::DW_TAG_common_block, "invalid tag", &N);
1316  if (auto *S = N.getRawScope())
1317  AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1318  if (auto *S = N.getRawDecl())
1319  AssertDI(isa<DIGlobalVariable>(S), "invalid declaration", &N, S);
1320 }
1321 
1322 void Verifier::visitDINamespace(const DINamespace &N) {
1323  AssertDI(N.getTag() == dwarf::DW_TAG_namespace, "invalid tag", &N);
1324  if (auto *S = N.getRawScope())
1325  AssertDI(isa<DIScope>(S), "invalid scope ref", &N, S);
1326 }
1327 
1328 void Verifier::visitDIMacro(const DIMacro &N) {
1329  AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_define ||
1330  N.getMacinfoType() == dwarf::DW_MACINFO_undef,
1331  "invalid macinfo type", &N);
1332  AssertDI(!N.getName().empty(), "anonymous macro", &N);
1333  if (!N.getValue().empty()) {
1334  assert(N.getValue().data()[0] != ' ' && "Macro value has a space prefix");
1335  }
1336 }
1337 
1338 void Verifier::visitDIMacroFile(const DIMacroFile &N) {
1339  AssertDI(N.getMacinfoType() == dwarf::DW_MACINFO_start_file,
1340  "invalid macinfo type", &N);
1341  if (auto *F = N.getRawFile())
1342  AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1343 
1344  if (auto *Array = N.getRawElements()) {
1345  AssertDI(isa<MDTuple>(Array), "invalid macro list", &N, Array);
1346  for (Metadata *Op : N.getElements()->operands()) {
1347  AssertDI(Op && isa<DIMacroNode>(Op), "invalid macro ref", &N, Op);
1348  }
1349  }
1350 }
1351 
1352 void Verifier::visitDIArgList(const DIArgList &N) {
1353  AssertDI(!N.getNumOperands(),
1354  "DIArgList should have no operands other than a list of "
1355  "ValueAsMetadata",
1356  &N);
1357 }
1358 
1359 void Verifier::visitDIModule(const DIModule &N) {
1360  AssertDI(N.getTag() == dwarf::DW_TAG_module, "invalid tag", &N);
1361  AssertDI(!N.getName().empty(), "anonymous module", &N);
1362 }
1363 
1364 void Verifier::visitDITemplateParameter(const DITemplateParameter &N) {
1365  AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1366 }
1367 
1368 void Verifier::visitDITemplateTypeParameter(const DITemplateTypeParameter &N) {
1369  visitDITemplateParameter(N);
1370 
1371  AssertDI(N.getTag() == dwarf::DW_TAG_template_type_parameter, "invalid tag",
1372  &N);
1373 }
1374 
1375 void Verifier::visitDITemplateValueParameter(
1376  const DITemplateValueParameter &N) {
1377  visitDITemplateParameter(N);
1378 
1379  AssertDI(N.getTag() == dwarf::DW_TAG_template_value_parameter ||
1380  N.getTag() == dwarf::DW_TAG_GNU_template_template_param ||
1381  N.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack,
1382  "invalid tag", &N);
1383 }
1384 
1385 void Verifier::visitDIVariable(const DIVariable &N) {
1386  if (auto *S = N.getRawScope())
1387  AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
1388  if (auto *F = N.getRawFile())
1389  AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1390 }
1391 
1392 void Verifier::visitDIGlobalVariable(const DIGlobalVariable &N) {
1393  // Checks common to all variables.
1394  visitDIVariable(N);
1395 
1396  AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1397  AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1398  // Assert only if the global variable is not an extern
1399  if (N.isDefinition())
1400  AssertDI(N.getType(), "missing global variable type", &N);
1401  if (auto *Member = N.getRawStaticDataMemberDeclaration()) {
1402  AssertDI(isa<DIDerivedType>(Member),
1403  "invalid static data member declaration", &N, Member);
1404  }
1405 }
1406 
1407 void Verifier::visitDILocalVariable(const DILocalVariable &N) {
1408  // Checks common to all variables.
1409  visitDIVariable(N);
1410 
1411  AssertDI(isType(N.getRawType()), "invalid type ref", &N, N.getRawType());
1412  AssertDI(N.getTag() == dwarf::DW_TAG_variable, "invalid tag", &N);
1413  AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1414  "local variable requires a valid scope", &N, N.getRawScope());
1415  if (auto Ty = N.getType())
1416  AssertDI(!isa<DISubroutineType>(Ty), "invalid type", &N, N.getType());
1417 }
1418 
1419 void Verifier::visitDILabel(const DILabel &N) {
1420  if (auto *S = N.getRawScope())
1421  AssertDI(isa<DIScope>(S), "invalid scope", &N, S);
1422  if (auto *F = N.getRawFile())
1423  AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1424 
1425  AssertDI(N.getTag() == dwarf::DW_TAG_label, "invalid tag", &N);
1426  AssertDI(N.getRawScope() && isa<DILocalScope>(N.getRawScope()),
1427  "label requires a valid scope", &N, N.getRawScope());
1428 }
1429 
1430 void Verifier::visitDIExpression(const DIExpression &N) {
1431  AssertDI(N.isValid(), "invalid expression", &N);
1432 }
1433 
1434 void Verifier::visitDIGlobalVariableExpression(
1435  const DIGlobalVariableExpression &GVE) {
1436  AssertDI(GVE.getVariable(), "missing variable");
1437  if (auto *Var = GVE.getVariable())
1438  visitDIGlobalVariable(*Var);
1439  if (auto *Expr = GVE.getExpression()) {
1440  visitDIExpression(*Expr);
1441  if (auto Fragment = Expr->getFragmentInfo())
1442  verifyFragmentExpression(*GVE.getVariable(), *Fragment, &GVE);
1443  }
1444 }
1445 
1446 void Verifier::visitDIObjCProperty(const DIObjCProperty &N) {
1447  AssertDI(N.getTag() == dwarf::DW_TAG_APPLE_property, "invalid tag", &N);
1448  if (auto *T = N.getRawType())
1449  AssertDI(isType(T), "invalid type ref", &N, T);
1450  if (auto *F = N.getRawFile())
1451  AssertDI(isa<DIFile>(F), "invalid file", &N, F);
1452 }
1453 
1454 void Verifier::visitDIImportedEntity(const DIImportedEntity &N) {
1455  AssertDI(N.getTag() == dwarf::DW_TAG_imported_module ||
1456  N.getTag() == dwarf::DW_TAG_imported_declaration,
1457  "invalid tag", &N);
1458  if (auto *S = N.getRawScope())
1459  AssertDI(isa<DIScope>(S), "invalid scope for imported entity", &N, S);
1460  AssertDI(isDINode(N.getRawEntity()), "invalid imported entity", &N,
1461  N.getRawEntity());
1462 }
1463 
1464 void Verifier::visitComdat(const Comdat &C) {
1465  // In COFF the Module is invalid if the GlobalValue has private linkage.
1466  // Entities with private linkage don't have entries in the symbol table.
1467  if (TT.isOSBinFormatCOFF())
1468  if (const GlobalValue *GV = M.getNamedValue(C.getName()))
1469  Assert(!GV->hasPrivateLinkage(),
1470  "comdat global value has private linkage", GV);
1471 }
1472 
1473 void Verifier::visitModuleIdents(const Module &M) {
1474  const NamedMDNode *Idents = M.getNamedMetadata("llvm.ident");
1475  if (!Idents)
1476  return;
1477 
1478  // llvm.ident takes a list of metadata entry. Each entry has only one string.
1479  // Scan each llvm.ident entry and make sure that this requirement is met.
1480  for (const MDNode *N : Idents->operands()) {
1481  Assert(N->getNumOperands() == 1,
1482  "incorrect number of operands in llvm.ident metadata", N);
1483  Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
1484  ("invalid value for llvm.ident metadata entry operand"
1485  "(the operand should be a string)"),
1486  N->getOperand(0));
1487  }
1488 }
1489 
1490 void Verifier::visitModuleCommandLines(const Module &M) {
1491  const NamedMDNode *CommandLines = M.getNamedMetadata("llvm.commandline");
1492  if (!CommandLines)
1493  return;
1494 
1495  // llvm.commandline takes a list of metadata entry. Each entry has only one
1496  // string. Scan each llvm.commandline entry and make sure that this
1497  // requirement is met.
1498  for (const MDNode *N : CommandLines->operands()) {
1499  Assert(N->getNumOperands() == 1,
1500  "incorrect number of operands in llvm.commandline metadata", N);
1501  Assert(dyn_cast_or_null<MDString>(N->getOperand(0)),
1502  ("invalid value for llvm.commandline metadata entry operand"
1503  "(the operand should be a string)"),
1504  N->getOperand(0));
1505  }
1506 }
1507 
1508 void Verifier::visitModuleFlags(const Module &M) {
1509  const NamedMDNode *Flags = M.getModuleFlagsMetadata();
1510  if (!Flags) return;
1511 
1512  // Scan each flag, and track the flags and requirements.
1514  SmallVector<const MDNode*, 16> Requirements;
1515  for (const MDNode *MDN : Flags->operands())
1516  visitModuleFlag(MDN, SeenIDs, Requirements);
1517 
1518  // Validate that the requirements in the module are valid.
1519  for (const MDNode *Requirement : Requirements) {
1520  const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
1521  const Metadata *ReqValue = Requirement->getOperand(1);
1522 
1523  const MDNode *Op = SeenIDs.lookup(Flag);
1524  if (!Op) {
1525  CheckFailed("invalid requirement on flag, flag is not present in module",
1526  Flag);
1527  continue;
1528  }
1529 
1530  if (Op->getOperand(2) != ReqValue) {
1531  CheckFailed(("invalid requirement on flag, "
1532  "flag does not have the required value"),
1533  Flag);
1534  continue;
1535  }
1536  }
1537 }
1538 
1539 void
1540 Verifier::visitModuleFlag(const MDNode *Op,
1542  SmallVectorImpl<const MDNode *> &Requirements) {
1543  // Each module flag should have three arguments, the merge behavior (a
1544  // constant int), the flag ID (an MDString), and the value.
1545  Assert(Op->getNumOperands() == 3,
1546  "incorrect number of operands in module flag", Op);
1548  if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
1549  Assert(
1550  mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0)),
1551  "invalid behavior operand in module flag (expected constant integer)",
1552  Op->getOperand(0));
1553  Assert(false,
1554  "invalid behavior operand in module flag (unexpected constant)",
1555  Op->getOperand(0));
1556  }
1557  MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
1558  Assert(ID, "invalid ID operand in module flag (expected metadata string)",
1559  Op->getOperand(1));
1560 
1561  // Sanity check the values for behaviors with additional requirements.
1562  switch (MFB) {
1563  case Module::Error:
1564  case Module::Warning:
1565  case Module::Override:
1566  // These behavior types accept any value.
1567  break;
1568 
1569  case Module::Max: {
1570  Assert(mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2)),
1571  "invalid value for 'max' module flag (expected constant integer)",
1572  Op->getOperand(2));
1573  break;
1574  }
1575 
1576  case Module::Require: {
1577  // The value should itself be an MDNode with two operands, a flag ID (an
1578  // MDString), and a value.
1579  MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
1580  Assert(Value && Value->getNumOperands() == 2,
1581  "invalid value for 'require' module flag (expected metadata pair)",
1582  Op->getOperand(2));
1583  Assert(isa<MDString>(Value->getOperand(0)),
1584  ("invalid value for 'require' module flag "
1585  "(first value operand should be a string)"),
1586  Value->getOperand(0));
1587 
1588  // Append it to the list of requirements, to check once all module flags are
1589  // scanned.
1590  Requirements.push_back(Value);
1591  break;
1592  }
1593 
1594  case Module::Append:
1595  case Module::AppendUnique: {
1596  // These behavior types require the operand be an MDNode.
1597  Assert(isa<MDNode>(Op->getOperand(2)),
1598  "invalid value for 'append'-type module flag "
1599  "(expected a metadata node)",
1600  Op->getOperand(2));
1601  break;
1602  }
1603  }
1604 
1605  // Unless this is a "requires" flag, check the ID is unique.
1606  if (MFB != Module::Require) {
1607  bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
1608  Assert(Inserted,
1609  "module flag identifiers must be unique (or of 'require' type)", ID);
1610  }
1611 
1612  if (ID->getString() == "wchar_size") {
1614  = mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1615  Assert(Value, "wchar_size metadata requires constant integer argument");
1616  }
1617 
1618  if (ID->getString() == "Linker Options") {
1619  // If the llvm.linker.options named metadata exists, we assume that the
1620  // bitcode reader has upgraded the module flag. Otherwise the flag might
1621  // have been created by a client directly.
1622  Assert(M.getNamedMetadata("llvm.linker.options"),
1623  "'Linker Options' named metadata no longer supported");
1624  }
1625 
1626  if (ID->getString() == "SemanticInterposition") {
1627  ConstantInt *Value =
1628  mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
1629  Assert(Value,
1630  "SemanticInterposition metadata requires constant integer argument");
1631  }
1632 
1633  if (ID->getString() == "CG Profile") {
1634  for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
1635  visitModuleFlagCGProfileEntry(MDO);
1636  }
1637 }
1638 
1639 void Verifier::visitModuleFlagCGProfileEntry(const MDOperand &MDO) {
1640  auto CheckFunction = [&](const MDOperand &FuncMDO) {
1641  if (!FuncMDO)
1642  return;
1643  auto F = dyn_cast<ValueAsMetadata>(FuncMDO);
1644  Assert(F && isa<Function>(F->getValue()->stripPointerCasts()),
1645  "expected a Function or null", FuncMDO);
1646  };
1647  auto Node = dyn_cast_or_null<MDNode>(MDO);
1648  Assert(Node && Node->getNumOperands() == 3, "expected a MDNode triple", MDO);
1649  CheckFunction(Node->getOperand(0));
1650  CheckFunction(Node->getOperand(1));
1651  auto Count = dyn_cast_or_null<ConstantAsMetadata>(Node->getOperand(2));
1652  Assert(Count && Count->getType()->isIntegerTy(),
1653  "expected an integer constant", Node->getOperand(2));
1654 }
1655 
1656 void Verifier::verifyAttributeTypes(AttributeSet Attrs, const Value *V) {
1657  for (Attribute A : Attrs) {
1658 
1659  if (A.isStringAttribute()) {
1660 #define GET_ATTR_NAMES
1661 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME)
1662 #define ATTRIBUTE_STRBOOL(ENUM_NAME, DISPLAY_NAME) \
1663  if (A.getKindAsString() == #DISPLAY_NAME) { \
1664  auto V = A.getValueAsString(); \
1665  if (!(V.empty() || V == "true" || V == "false")) \
1666  CheckFailed("invalid value for '" #DISPLAY_NAME "' attribute: " + V + \
1667  ""); \
1668  }
1669 
1670 #include "llvm/IR/Attributes.inc"
1671  continue;
1672  }
1673 
1674  if (A.isIntAttribute() != Attribute::isIntAttrKind(A.getKindAsEnum())) {
1675  CheckFailed("Attribute '" + A.getAsString() + "' should have an Argument",
1676  V);
1677  return;
1678  }
1679  }
1680 }
1681 
1682 // VerifyParameterAttrs - Check the given attributes for an argument or return
1683 // value of the specified type. The value V is printed in error messages.
1684 void Verifier::verifyParameterAttrs(AttributeSet Attrs, Type *Ty,
1685  const Value *V) {
1686  if (!Attrs.hasAttributes())
1687  return;
1688 
1689  verifyAttributeTypes(Attrs, V);
1690 
1691  for (Attribute Attr : Attrs)
1692  Assert(Attr.isStringAttribute() ||
1693  Attribute::canUseAsParamAttr(Attr.getKindAsEnum()),
1694  "Attribute '" + Attr.getAsString() +
1695  "' does not apply to parameters",
1696  V);
1697 
1698  if (Attrs.hasAttribute(Attribute::ImmArg)) {
1699  Assert(Attrs.getNumAttributes() == 1,
1700  "Attribute 'immarg' is incompatible with other attributes", V);
1701  }
1702 
1703  // Check for mutually incompatible attributes. Only inreg is compatible with
1704  // sret.
1705  unsigned AttrCount = 0;
1706  AttrCount += Attrs.hasAttribute(Attribute::ByVal);
1707  AttrCount += Attrs.hasAttribute(Attribute::InAlloca);
1708  AttrCount += Attrs.hasAttribute(Attribute::Preallocated);
1709  AttrCount += Attrs.hasAttribute(Attribute::StructRet) ||
1710  Attrs.hasAttribute(Attribute::InReg);
1711  AttrCount += Attrs.hasAttribute(Attribute::Nest);
1712  AttrCount += Attrs.hasAttribute(Attribute::ByRef);
1713  Assert(AttrCount <= 1,
1714  "Attributes 'byval', 'inalloca', 'preallocated', 'inreg', 'nest', "
1715  "'byref', and 'sret' are incompatible!",
1716  V);
1717 
1718  Assert(!(Attrs.hasAttribute(Attribute::InAlloca) &&
1720  "Attributes "
1721  "'inalloca and readonly' are incompatible!",
1722  V);
1723 
1724  Assert(!(Attrs.hasAttribute(Attribute::StructRet) &&
1725  Attrs.hasAttribute(Attribute::Returned)),
1726  "Attributes "
1727  "'sret and returned' are incompatible!",
1728  V);
1729 
1730  Assert(!(Attrs.hasAttribute(Attribute::ZExt) &&
1731  Attrs.hasAttribute(Attribute::SExt)),
1732  "Attributes "
1733  "'zeroext and signext' are incompatible!",
1734  V);
1735 
1736  Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1738  "Attributes "
1739  "'readnone and readonly' are incompatible!",
1740  V);
1741 
1742  Assert(!(Attrs.hasAttribute(Attribute::ReadNone) &&
1743  Attrs.hasAttribute(Attribute::WriteOnly)),
1744  "Attributes "
1745  "'readnone and writeonly' are incompatible!",
1746  V);
1747 
1749  Attrs.hasAttribute(Attribute::WriteOnly)),
1750  "Attributes "
1751  "'readonly and writeonly' are incompatible!",
1752  V);
1753 
1754  Assert(!(Attrs.hasAttribute(Attribute::NoInline) &&
1755  Attrs.hasAttribute(Attribute::AlwaysInline)),
1756  "Attributes "
1757  "'noinline and alwaysinline' are incompatible!",
1758  V);
1759 
1760  AttrBuilder IncompatibleAttrs = AttributeFuncs::typeIncompatible(Ty);
1761  for (Attribute Attr : Attrs) {
1762  if (!Attr.isStringAttribute() &&
1763  IncompatibleAttrs.contains(Attr.getKindAsEnum())) {
1764  CheckFailed("Attribute '" + Attr.getAsString() +
1765  "' applied to incompatible type!", V);
1766  return;
1767  }
1768  }
1769 
1770  if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
1771  if (Attrs.hasAttribute(Attribute::ByVal)) {
1772  SmallPtrSet<Type *, 4> Visited;
1773  Assert(Attrs.getByValType()->isSized(&Visited),
1774  "Attribute 'byval' does not support unsized types!", V);
1775  }
1776  if (Attrs.hasAttribute(Attribute::ByRef)) {
1777  SmallPtrSet<Type *, 4> Visited;
1778  Assert(Attrs.getByRefType()->isSized(&Visited),
1779  "Attribute 'byref' does not support unsized types!", V);
1780  }
1781  if (Attrs.hasAttribute(Attribute::InAlloca)) {
1782  SmallPtrSet<Type *, 4> Visited;
1783  Assert(Attrs.getInAllocaType()->isSized(&Visited),
1784  "Attribute 'inalloca' does not support unsized types!", V);
1785  }
1786  if (Attrs.hasAttribute(Attribute::Preallocated)) {
1787  SmallPtrSet<Type *, 4> Visited;
1788  Assert(Attrs.getPreallocatedType()->isSized(&Visited),
1789  "Attribute 'preallocated' does not support unsized types!", V);
1790  }
1791  if (!PTy->isOpaque()) {
1792  if (!isa<PointerType>(PTy->getElementType()))
1793  Assert(!Attrs.hasAttribute(Attribute::SwiftError),
1794  "Attribute 'swifterror' only applies to parameters "
1795  "with pointer to pointer type!",
1796  V);
1797  if (Attrs.hasAttribute(Attribute::ByRef)) {
1798  Assert(Attrs.getByRefType() == PTy->getElementType(),
1799  "Attribute 'byref' type does not match parameter!", V);
1800  }
1801 
1802  if (Attrs.hasAttribute(Attribute::ByVal) && Attrs.getByValType()) {
1803  Assert(Attrs.getByValType() == PTy->getElementType(),
1804  "Attribute 'byval' type does not match parameter!", V);
1805  }
1806 
1807  if (Attrs.hasAttribute(Attribute::Preallocated)) {
1808  Assert(Attrs.getPreallocatedType() == PTy->getElementType(),
1809  "Attribute 'preallocated' type does not match parameter!", V);
1810  }
1811 
1812  if (Attrs.hasAttribute(Attribute::InAlloca)) {
1813  Assert(Attrs.getInAllocaType() == PTy->getElementType(),
1814  "Attribute 'inalloca' type does not match parameter!", V);
1815  }
1816 
1817  if (Attrs.hasAttribute(Attribute::ElementType)) {
1818  Assert(Attrs.getElementType() == PTy->getElementType(),
1819  "Attribute 'elementtype' type does not match parameter!", V);
1820  }
1821  }
1822  }
1823 }
1824 
1825 void Verifier::checkUnsignedBaseTenFuncAttr(AttributeList Attrs, StringRef Attr,
1826  const Value *V) {
1827  if (Attrs.hasFnAttribute(Attr)) {
1829  .getValueAsString();
1830  unsigned N;
1831  if (S.getAsInteger(10, N))
1832  CheckFailed("\"" + Attr + "\" takes an unsigned integer: " + S, V);
1833  }
1834 }
1835 
1836 // Check parameter attributes against a function type.
1837 // The value V is printed in error messages.
1838 void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
1839  const Value *V, bool IsIntrinsic) {
1840  if (Attrs.isEmpty())
1841  return;
1842 
1843  if (AttributeListsVisited.insert(Attrs.getRawPointer()).second) {
1844  Assert(Attrs.hasParentContext(Context),
1845  "Attribute list does not match Module context!", &Attrs, V);
1846  for (const auto &AttrSet : Attrs) {
1847  Assert(!AttrSet.hasAttributes() || AttrSet.hasParentContext(Context),
1848  "Attribute set does not match Module context!", &AttrSet, V);
1849  for (const auto &A : AttrSet) {
1850  Assert(A.hasParentContext(Context),
1851  "Attribute does not match Module context!", &A, V);
1852  }
1853  }
1854  }
1855 
1856  bool SawNest = false;
1857  bool SawReturned = false;
1858  bool SawSRet = false;
1859  bool SawSwiftSelf = false;
1860  bool SawSwiftAsync = false;
1861  bool SawSwiftError = false;
1862 
1863  // Verify return value attributes.
1864  AttributeSet RetAttrs = Attrs.getRetAttributes();
1865  for (Attribute RetAttr : RetAttrs)
1866  Assert(RetAttr.isStringAttribute() ||
1867  Attribute::canUseAsRetAttr(RetAttr.getKindAsEnum()),
1868  "Attribute '" + RetAttr.getAsString() +
1869  "' does not apply to function return values",
1870  V);
1871 
1872  verifyParameterAttrs(RetAttrs, FT->getReturnType(), V);
1873 
1874  // Verify parameter attributes.
1875  for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) {
1876  Type *Ty = FT->getParamType(i);
1877  AttributeSet ArgAttrs = Attrs.getParamAttributes(i);
1878 
1879  if (!IsIntrinsic) {
1880  Assert(!ArgAttrs.hasAttribute(Attribute::ImmArg),
1881  "immarg attribute only applies to intrinsics",V);
1882  Assert(!ArgAttrs.hasAttribute(Attribute::ElementType),
1883  "Attribute 'elementtype' can only be applied to intrinsics.", V);
1884  }
1885 
1886  verifyParameterAttrs(ArgAttrs, Ty, V);
1887 
1888  if (ArgAttrs.hasAttribute(Attribute::Nest)) {
1889  Assert(!SawNest, "More than one parameter has attribute nest!", V);
1890  SawNest = true;
1891  }
1892 
1893  if (ArgAttrs.hasAttribute(Attribute::Returned)) {
1894  Assert(!SawReturned, "More than one parameter has attribute returned!",
1895  V);
1897  "Incompatible argument and return types for 'returned' attribute",
1898  V);
1899  SawReturned = true;
1900  }
1901 
1902  if (ArgAttrs.hasAttribute(Attribute::StructRet)) {
1903  Assert(!SawSRet, "Cannot have multiple 'sret' parameters!", V);
1904  Assert(i == 0 || i == 1,
1905  "Attribute 'sret' is not on first or second parameter!", V);
1906  SawSRet = true;
1907  }
1908 
1909  if (ArgAttrs.hasAttribute(Attribute::SwiftSelf)) {
1910  Assert(!SawSwiftSelf, "Cannot have multiple 'swiftself' parameters!", V);
1911  SawSwiftSelf = true;
1912  }
1913 
1914  if (ArgAttrs.hasAttribute(Attribute::SwiftAsync)) {
1915  Assert(!SawSwiftAsync, "Cannot have multiple 'swiftasync' parameters!", V);
1916  SawSwiftAsync = true;
1917  }
1918 
1919  if (ArgAttrs.hasAttribute(Attribute::SwiftError)) {
1920  Assert(!SawSwiftError, "Cannot have multiple 'swifterror' parameters!",
1921  V);
1922  SawSwiftError = true;
1923  }
1924 
1925  if (ArgAttrs.hasAttribute(Attribute::InAlloca)) {
1926  Assert(i == FT->getNumParams() - 1,
1927  "inalloca isn't on the last parameter!", V);
1928  }
1929  }
1930 
1931  if (!Attrs.hasAttributes(AttributeList::FunctionIndex))
1932  return;
1933 
1934  verifyAttributeTypes(Attrs.getFnAttributes(), V);
1935  for (Attribute FnAttr : Attrs.getFnAttributes())
1936  Assert(FnAttr.isStringAttribute() ||
1937  Attribute::canUseAsFnAttr(FnAttr.getKindAsEnum()),
1938  "Attribute '" + FnAttr.getAsString() +
1939  "' does not apply to functions!",
1940  V);
1941 
1942  Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1944  "Attributes 'readnone and readonly' are incompatible!", V);
1945 
1946  Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1947  Attrs.hasFnAttribute(Attribute::WriteOnly)),
1948  "Attributes 'readnone and writeonly' are incompatible!", V);
1949 
1951  Attrs.hasFnAttribute(Attribute::WriteOnly)),
1952  "Attributes 'readonly and writeonly' are incompatible!", V);
1953 
1954  Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1955  Attrs.hasFnAttribute(Attribute::InaccessibleMemOrArgMemOnly)),
1956  "Attributes 'readnone and inaccessiblemem_or_argmemonly' are "
1957  "incompatible!",
1958  V);
1959 
1960  Assert(!(Attrs.hasFnAttribute(Attribute::ReadNone) &&
1961  Attrs.hasFnAttribute(Attribute::InaccessibleMemOnly)),
1962  "Attributes 'readnone and inaccessiblememonly' are incompatible!", V);
1963 
1964  Assert(!(Attrs.hasFnAttribute(Attribute::NoInline) &&
1965  Attrs.hasFnAttribute(Attribute::AlwaysInline)),
1966  "Attributes 'noinline and alwaysinline' are incompatible!", V);
1967 
1968  if (Attrs.hasFnAttribute(Attribute::OptimizeNone)) {
1969  Assert(Attrs.hasFnAttribute(Attribute::NoInline),
1970  "Attribute 'optnone' requires 'noinline'!", V);
1971 
1972  Assert(!Attrs.hasFnAttribute(Attribute::OptimizeForSize),
1973  "Attributes 'optsize and optnone' are incompatible!", V);
1974 
1975  Assert(!Attrs.hasFnAttribute(Attribute::MinSize),
1976  "Attributes 'minsize and optnone' are incompatible!", V);
1977  }
1978 
1980  const GlobalValue *GV = cast<GlobalValue>(V);
1982  "Attribute 'jumptable' requires 'unnamed_addr'", V);
1983  }
1984 
1985  if (Attrs.hasFnAttribute(Attribute::AllocSize)) {
1986  std::pair<unsigned, Optional<unsigned>> Args =
1987  Attrs.getAllocSizeArgs(AttributeList::FunctionIndex);
1988 
1989  auto CheckParam = [&](StringRef Name, unsigned ParamNo) {
1990  if (ParamNo >= FT->getNumParams()) {
1991  CheckFailed("'allocsize' " + Name + " argument is out of bounds", V);
1992  return false;
1993  }
1994 
1995  if (!FT->getParamType(ParamNo)->isIntegerTy()) {
1996  CheckFailed("'allocsize' " + Name +
1997  " argument must refer to an integer parameter",
1998  V);
1999  return false;
2000  }
2001 
2002  return true;
2003  };
2004 
2005  if (!CheckParam("element size", Args.first))
2006  return;
2007 
2008  if (Args.second && !CheckParam("number of elements", *Args.second))
2009  return;
2010  }
2011 
2012  if (Attrs.hasFnAttribute(Attribute::VScaleRange)) {
2013  std::pair<unsigned, unsigned> Args =
2014  Attrs.getVScaleRangeArgs(AttributeList::FunctionIndex);
2015 
2016  if (Args.first > Args.second && Args.second != 0)
2017  CheckFailed("'vscale_range' minimum cannot be greater than maximum", V);
2018  }
2019 
2020  if (Attrs.hasFnAttribute("frame-pointer")) {
2022  "frame-pointer").getValueAsString();
2023  if (FP != "all" && FP != "non-leaf" && FP != "none")
2024  CheckFailed("invalid value for 'frame-pointer' attribute: " + FP, V);
2025  }
2026 
2027  checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V);
2028  checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
2029  checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);
2030 }
2031 
2032 void Verifier::verifyFunctionMetadata(
2033  ArrayRef<std::pair<unsigned, MDNode *>> MDs) {
2034  for (const auto &Pair : MDs) {
2035  if (Pair.first == LLVMContext::MD_prof) {
2036  MDNode *MD = Pair.second;
2037  Assert(MD->getNumOperands() >= 2,
2038  "!prof annotations should have no less than 2 operands", MD);
2039 
2040  // Check first operand.
2041  Assert(MD->getOperand(0) != nullptr, "first operand should not be null",
2042  MD);
2043  Assert(isa<MDString>(MD->getOperand(0)),
2044  "expected string with name of the !prof annotation", MD);
2045  MDString *MDS = cast<MDString>(MD->getOperand(0));
2046  StringRef ProfName = MDS->getString();
2047  Assert(ProfName.equals("function_entry_count") ||
2048  ProfName.equals("synthetic_function_entry_count"),
2049  "first operand should be 'function_entry_count'"
2050  " or 'synthetic_function_entry_count'",
2051  MD);
2052 
2053  // Check second operand.
2054  Assert(MD->getOperand(1) != nullptr, "second operand should not be null",
2055  MD);
2056  Assert(isa<ConstantAsMetadata>(MD->getOperand(1)),
2057  "expected integer argument to function_entry_count", MD);
2058  }
2059  }
2060 }
2061 
2062 void Verifier::visitConstantExprsRecursively(const Constant *EntryC) {
2063  if (!ConstantExprVisited.insert(EntryC).second)
2064  return;
2065 
2067  Stack.push_back(EntryC);
2068 
2069  while (!Stack.empty()) {
2070  const Constant *C = Stack.pop_back_val();
2071 
2072  // Check this constant expression.
2073  if (const auto *CE = dyn_cast<ConstantExpr>(C))
2074  visitConstantExpr(CE);
2075 
2076  if (const auto *GV = dyn_cast<GlobalValue>(C)) {
2077  // Global Values get visited separately, but we do need to make sure
2078  // that the global value is in the correct module
2079  Assert(GV->getParent() == &M, "Referencing global in another module!",
2080  EntryC, &M, GV, GV->getParent());
2081  continue;
2082  }
2083 
2084  // Visit all sub-expressions.
2085  for (const Use &U : C->operands()) {
2086  const auto *OpC = dyn_cast<Constant>(U);
2087  if (!OpC)
2088  continue;
2089  if (!ConstantExprVisited.insert(OpC).second)
2090  continue;
2091  Stack.push_back(OpC);
2092  }
2093  }
2094 }
2095 
2096 void Verifier::visitConstantExpr(const ConstantExpr *CE) {
2097  if (CE->getOpcode() == Instruction::BitCast)
2098  Assert(CastInst::castIsValid(Instruction::BitCast, CE->getOperand(0),
2099  CE->getType()),
2100  "Invalid bitcast", CE);
2101 }
2102 
2103 bool Verifier::verifyAttributeCount(AttributeList Attrs, unsigned Params) {
2104  // There shouldn't be more attribute sets than there are parameters plus the
2105  // function and return value.
2106  return Attrs.getNumAttrSets() <= Params + 2;
2107 }
2108 
2109 /// Verify that statepoint intrinsic is well formed.
2110 void Verifier::verifyStatepoint(const CallBase &Call) {
2111  assert(Call.getCalledFunction() &&
2112  Call.getCalledFunction()->getIntrinsicID() ==
2113  Intrinsic::experimental_gc_statepoint);
2114 
2115  Assert(!Call.doesNotAccessMemory() && !Call.onlyReadsMemory() &&
2116  !Call.onlyAccessesArgMemory(),
2117  "gc.statepoint must read and write all memory to preserve "
2118  "reordering restrictions required by safepoint semantics",
2119  Call);
2120 
2121  const int64_t NumPatchBytes =
2122  cast<ConstantInt>(Call.getArgOperand(1))->getSExtValue();
2123  assert(isInt<32>(NumPatchBytes) && "NumPatchBytesV is an i32!");
2124  Assert(NumPatchBytes >= 0,
2125  "gc.statepoint number of patchable bytes must be "
2126  "positive",
2127  Call);
2128 
2129  const Value *Target = Call.getArgOperand(2);
2130  auto *PT = dyn_cast<PointerType>(Target->getType());
2131  Assert(PT && PT->getElementType()->isFunctionTy(),
2132  "gc.statepoint callee must be of function pointer type", Call, Target);
2133  FunctionType *TargetFuncType = cast<FunctionType>(PT->getElementType());
2134 
2135  const int NumCallArgs = cast<ConstantInt>(Call.getArgOperand(3))->getZExtValue();
2136  Assert(NumCallArgs >= 0,
2137  "gc.statepoint number of arguments to underlying call "
2138  "must be positive",
2139  Call);
2140  const int NumParams = (int)TargetFuncType->getNumParams();
2141  if (TargetFuncType->isVarArg()) {
2142  Assert(NumCallArgs >= NumParams,
2143  "gc.statepoint mismatch in number of vararg call args", Call);
2144 
2145  // TODO: Remove this limitation
2146  Assert(TargetFuncType->getReturnType()->isVoidTy(),
2147  "gc.statepoint doesn't support wrapping non-void "
2148  "vararg functions yet",
2149  Call);
2150  } else
2151  Assert(NumCallArgs == NumParams,
2152  "gc.statepoint mismatch in number of call args", Call);
2153 
2154  const uint64_t Flags
2155  = cast<ConstantInt>(Call.getArgOperand(4))->getZExtValue();
2156  Assert((Flags & ~(uint64_t)StatepointFlags::MaskAll) == 0,
2157  "unknown flag used in gc.statepoint flags argument", Call);
2158 
2159  // Verify that the types of the call parameter arguments match
2160  // the type of the wrapped callee.
2161  AttributeList Attrs = Call.getAttributes();
2162  for (int i = 0; i < NumParams; i++) {
2163  Type *ParamType = TargetFuncType->getParamType(i);
2164  Type *ArgType = Call.getArgOperand(5 + i)->getType();
2165  Assert(ArgType == ParamType,
2166  "gc.statepoint call argument does not match wrapped "
2167  "function type",
2168  Call);
2169 
2170  if (TargetFuncType->isVarArg()) {
2171  AttributeSet ArgAttrs = Attrs.getParamAttributes(5 + i);
2172  Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
2173  "Attribute 'sret' cannot be used for vararg call arguments!",
2174  Call);
2175  }
2176  }
2177 
2178  const int EndCallArgsInx = 4 + NumCallArgs;
2179 
2180  const Value *NumTransitionArgsV = Call.getArgOperand(EndCallArgsInx + 1);
2181  Assert(isa<ConstantInt>(NumTransitionArgsV),
2182  "gc.statepoint number of transition arguments "
2183  "must be constant integer",
2184  Call);
2185  const int NumTransitionArgs =
2186  cast<ConstantInt>(NumTransitionArgsV)->getZExtValue();
2187  Assert(NumTransitionArgs == 0,
2188  "gc.statepoint w/inline transition bundle is deprecated", Call);
2189  const int EndTransitionArgsInx = EndCallArgsInx + 1 + NumTransitionArgs;
2190 
2191  const Value *NumDeoptArgsV = Call.getArgOperand(EndTransitionArgsInx + 1);
2192  Assert(isa<ConstantInt>(NumDeoptArgsV),
2193  "gc.statepoint number of deoptimization arguments "
2194  "must be constant integer",
2195  Call);
2196  const int NumDeoptArgs = cast<ConstantInt>(NumDeoptArgsV)->getZExtValue();
2197  Assert(NumDeoptArgs == 0,
2198  "gc.statepoint w/inline deopt operands is deprecated", Call);
2199 
2200  const int ExpectedNumArgs = 7 + NumCallArgs;
2201  Assert(ExpectedNumArgs == (int)Call.arg_size(),
2202  "gc.statepoint too many arguments", Call);
2203 
2204  // Check that the only uses of this gc.statepoint are gc.result or
2205  // gc.relocate calls which are tied to this statepoint and thus part
2206  // of the same statepoint sequence
2207  for (const User *U : Call.users()) {
2208  const CallInst *UserCall = dyn_cast<const CallInst>(U);
2209  Assert(UserCall, "illegal use of statepoint token", Call, U);
2210  if (!UserCall)
2211  continue;
2212  Assert(isa<GCRelocateInst>(UserCall) || isa<GCResultInst>(UserCall),
2213  "gc.result or gc.relocate are the only value uses "
2214  "of a gc.statepoint",
2215  Call, U);
2216  if (isa<GCResultInst>(UserCall)) {
2217  Assert(UserCall->getArgOperand(0) == &Call,
2218  "gc.result connected to wrong gc.statepoint", Call, UserCall);
2219  } else if (isa<GCRelocateInst>(Call)) {
2220  Assert(UserCall->getArgOperand(0) == &Call,
2221  "gc.relocate connected to wrong gc.statepoint", Call, UserCall);
2222  }
2223  }
2224 
2225  // Note: It is legal for a single derived pointer to be listed multiple
2226  // times. It's non-optimal, but it is legal. It can also happen after
2227  // insertion if we strip a bitcast away.
2228  // Note: It is really tempting to check that each base is relocated and
2229  // that a derived pointer is never reused as a base pointer. This turns
2230  // out to be problematic since optimizations run after safepoint insertion
2231  // can recognize equality properties that the insertion logic doesn't know
2232  // about. See example statepoint.ll in the verifier subdirectory
2233 }
2234 
2235 void Verifier::verifyFrameRecoverIndices() {
2236  for (auto &Counts : FrameEscapeInfo) {
2237  Function *F = Counts.first;
2238  unsigned EscapedObjectCount = Counts.second.first;
2239  unsigned MaxRecoveredIndex = Counts.second.second;
2240  Assert(MaxRecoveredIndex <= EscapedObjectCount,
2241  "all indices passed to llvm.localrecover must be less than the "
2242  "number of arguments passed to llvm.localescape in the parent "
2243  "function",
2244  F);
2245  }
2246 }
2247 
2249  BasicBlock *UnwindDest;
2250  if (auto *II = dyn_cast<InvokeInst>(Terminator))
2251  UnwindDest = II->getUnwindDest();
2252  else if (auto *CSI = dyn_cast<CatchSwitchInst>(Terminator))
2253  UnwindDest = CSI->getUnwindDest();
2254  else
2255  UnwindDest = cast<CleanupReturnInst>(Terminator)->getUnwindDest();
2256  return UnwindDest->getFirstNonPHI();
2257 }
2258 
2259 void Verifier::verifySiblingFuncletUnwinds() {
2262  for (const auto &Pair : SiblingFuncletInfo) {
2263  Instruction *PredPad = Pair.first;
2264  if (Visited.count(PredPad))
2265  continue;
2266  Active.insert(PredPad);
2267  Instruction *Terminator = Pair.second;
2268  do {
2269  Instruction *SuccPad = getSuccPad(Terminator);
2270  if (Active.count(SuccPad)) {
2271  // Found a cycle; report error
2272  Instruction *CyclePad = SuccPad;
2273  SmallVector<Instruction *, 8> CycleNodes;
2274  do {
2275  CycleNodes.push_back(CyclePad);
2276  Instruction *CycleTerminator = SiblingFuncletInfo[CyclePad];
2277  if (CycleTerminator != CyclePad)
2278  CycleNodes.push_back(CycleTerminator);
2279  CyclePad = getSuccPad(CycleTerminator);
2280  } while (CyclePad != SuccPad);
2281  Assert(false, "EH pads can't handle each other's exceptions",
2282  ArrayRef<Instruction *>(CycleNodes));
2283  }
2284  // Don't re-walk a node we've already checked
2285  if (!Visited.insert(SuccPad).second)
2286  break;
2287  // Walk to this successor if it has a map entry.
2288  PredPad = SuccPad;
2289  auto TermI = SiblingFuncletInfo.find(PredPad);
2290  if (TermI == SiblingFuncletInfo.end())
2291  break;
2292  Terminator = TermI->second;
2293  Active.insert(PredPad);
2294  } while (true);
2295  // Each node only has one successor, so we've walked all the active
2296  // nodes' successors.
2297  Active.clear();
2298  }
2299 }
2300 
2301 // visitFunction - Verify that a function is ok.
2302 //
2303 void Verifier::visitFunction(const Function &F) {
2304  visitGlobalValue(F);
2305 
2306  // Check function arguments.
2307  FunctionType *FT = F.getFunctionType();
2308  unsigned NumArgs = F.arg_size();
2309 
2310  Assert(&Context == &F.getContext(),
2311  "Function context does not match Module context!", &F);
2312 
2313  Assert(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
2314  Assert(FT->getNumParams() == NumArgs,
2315  "# formal arguments must match # of arguments for function type!", &F,
2316  FT);
2317  Assert(F.getReturnType()->isFirstClassType() ||
2318  F.getReturnType()->isVoidTy() || F.getReturnType()->isStructTy(),
2319  "Functions cannot return aggregate values!", &F);
2320 
2321  Assert(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
2322  "Invalid struct return type!", &F);
2323 
2324  AttributeList Attrs = F.getAttributes();
2325 
2326  Assert(verifyAttributeCount(Attrs, FT->getNumParams()),
2327  "Attribute after last parameter!", &F);
2328 
2329  bool IsIntrinsic = F.isIntrinsic();
2330 
2331  // Check function attributes.
2332  verifyFunctionAttrs(FT, Attrs, &F, IsIntrinsic);
2333 
2334  // On function declarations/definitions, we do not support the builtin
2335  // attribute. We do not check this in VerifyFunctionAttrs since that is
2336  // checking for Attributes that can/can not ever be on functions.
2337  Assert(!Attrs.hasFnAttribute(Attribute::Builtin),
2338  "Attribute 'builtin' can only be applied to a callsite.", &F);
2339 
2340  Assert(!Attrs.hasAttrSomewhere(Attribute::ElementType),
2341  "Attribute 'elementtype' can only be applied to a callsite.", &F);
2342 
2343  // Check that this function meets the restrictions on this calling convention.
2344  // Sometimes varargs is used for perfectly forwarding thunks, so some of these
2345  // restrictions can be lifted.
2346  switch (F.getCallingConv()) {
2347  default:
2348  case CallingConv::C:
2349  break;
2350  case CallingConv::X86_INTR: {
2351  Assert(F.arg_empty() || Attrs.hasParamAttribute(0, Attribute::ByVal),
2352  "Calling convention parameter requires byval", &F);
2353  break;
2354  }
2357  Assert(F.getReturnType()->isVoidTy(),
2358  "Calling convention requires void return type", &F);
2365  Assert(!F.hasStructRetAttr(),
2366  "Calling convention does not allow sret", &F);
2367  if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
2368  const unsigned StackAS = DL.getAllocaAddrSpace();
2369  unsigned i = 0;
2370  for (const Argument &Arg : F.args()) {
2371  Assert(!Attrs.hasParamAttribute(i, Attribute::ByVal),
2372  "Calling convention disallows byval", &F);
2373  Assert(!Attrs.hasParamAttribute(i, Attribute::Preallocated),
2374  "Calling convention disallows preallocated", &F);
2375  Assert(!Attrs.hasParamAttribute(i, Attribute::InAlloca),
2376  "Calling convention disallows inalloca", &F);
2377 
2378  if (Attrs.hasParamAttribute(i, Attribute::ByRef)) {
2379  // FIXME: Should also disallow LDS and GDS, but we don't have the enum
2380  // value here.
2381  Assert(Arg.getType()->getPointerAddressSpace() != StackAS,
2382  "Calling convention disallows stack byref", &F);
2383  }
2384 
2385  ++i;
2386  }
2387  }
2388 
2390  case CallingConv::Fast:
2391  case CallingConv::Cold:
2395  Assert(!F.isVarArg(), "Calling convention does not support varargs or "
2396  "perfect forwarding!",
2397  &F);
2398  break;
2399  }
2400 
2401  // Check that the argument values match the function type for this function...
2402  unsigned i = 0;
2403  for (const Argument &Arg : F.args()) {
2404  Assert(Arg.getType() == FT->getParamType(i),
2405  "Argument value does not match function argument type!", &Arg,
2406  FT->getParamType(i));
2407  Assert(Arg.getType()->isFirstClassType(),
2408  "Function arguments must have first-class types!", &Arg);
2409  if (!IsIntrinsic) {
2410  Assert(!Arg.getType()->isMetadataTy(),
2411  "Function takes metadata but isn't an intrinsic", &Arg, &F);
2412  Assert(!Arg.getType()->isTokenTy(),
2413  "Function takes token but isn't an intrinsic", &Arg, &F);
2414  Assert(!Arg.getType()->isX86_AMXTy(),
2415  "Function takes x86_amx but isn't an intrinsic", &Arg, &F);
2416  }
2417 
2418  // Check that swifterror argument is only used by loads and stores.
2419  if (Attrs.hasParamAttribute(i, Attribute::SwiftError)) {
2420  verifySwiftErrorValue(&Arg);
2421  }
2422  ++i;
2423  }
2424 
2425  if (!IsIntrinsic) {
2426  Assert(!F.getReturnType()->isTokenTy(),
2427  "Function returns a token but isn't an intrinsic", &F);
2428  Assert(!F.getReturnType()->isX86_AMXTy(),
2429  "Function returns a x86_amx but isn't an intrinsic", &F);
2430  }
2431 
2432  // Get the function metadata attachments.
2434  F.getAllMetadata(MDs);
2435  assert(F.hasMetadata() != MDs.empty() && "Bit out-of-sync");
2436  verifyFunctionMetadata(MDs);
2437 
2438  // Check validity of the personality function
2439  if (F.hasPersonalityFn()) {
2440  auto *Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
2441  if (Per)
2442  Assert(Per->getParent() == F.getParent(),
2443  "Referencing personality function in another module!",
2444  &F, F.getParent(), Per, Per->getParent());
2445  }
2446 
2447  if (F.isMaterializable()) {
2448  // Function has a body somewhere we can't see.
2449  Assert(MDs.empty(), "unmaterialized function cannot have metadata", &F,
2450  MDs.empty() ? nullptr : MDs.front().second);
2451  } else if (F.isDeclaration()) {
2452  for (const auto &I : MDs) {
2453  // This is used for call site debug information.
2454  AssertDI(I.first != LLVMContext::MD_dbg ||
2455  !cast<DISubprogram>(I.second)->isDistinct(),
2456  "function declaration may only have a unique !dbg attachment",
2457  &F);
2458  Assert(I.first != LLVMContext::MD_prof,
2459  "function declaration may not have a !prof attachment", &F);
2460 
2461  // Verify the metadata itself.
2462  visitMDNode(*I.second, AreDebugLocsAllowed::Yes);
2463  }
2464  Assert(!F.hasPersonalityFn(),
2465  "Function declaration shouldn't have a personality routine", &F);
2466  } else {
2467  // Verify that this function (which has a body) is not named "llvm.*". It
2468  // is not legal to define intrinsics.
2469  Assert(!IsIntrinsic, "llvm intrinsics cannot be defined!", &F);
2470 
2471  // Check the entry node
2472  const BasicBlock *Entry = &F.getEntryBlock();
2473  Assert(pred_empty(Entry),
2474  "Entry block to function must not have predecessors!", Entry);
2475 
2476  // The address of the entry block cannot be taken, unless it is dead.
2477  if (Entry->hasAddressTaken()) {
2478  Assert(!BlockAddress::lookup(Entry)->isConstantUsed(),
2479  "blockaddress may not be used with the entry block!", Entry);
2480  }
2481 
2482  unsigned NumDebugAttachments = 0, NumProfAttachments = 0;
2483  // Visit metadata attachments.
2484  for (const auto &I : MDs) {
2485  // Verify that the attachment is legal.
2486  auto AllowLocs = AreDebugLocsAllowed::No;
2487  switch (I.first) {
2488  default:
2489  break;
2490  case LLVMContext::MD_dbg: {
2491  ++NumDebugAttachments;
2492  AssertDI(NumDebugAttachments == 1,
2493  "function must have a single !dbg attachment", &F, I.second);
2494  AssertDI(isa<DISubprogram>(I.second),
2495  "function !dbg attachment must be a subprogram", &F, I.second);
2496  AssertDI(cast<DISubprogram>(I.second)->isDistinct(),
2497  "function definition may only have a distinct !dbg attachment",
2498  &F);
2499 
2500  auto *SP = cast<DISubprogram>(I.second);
2501  const Function *&AttachedTo = DISubprogramAttachments[SP];
2502  AssertDI(!AttachedTo || AttachedTo == &F,
2503  "DISubprogram attached to more than one function", SP, &F);
2504  AttachedTo = &F;
2505  AllowLocs = AreDebugLocsAllowed::Yes;
2506  break;
2507  }
2508  case LLVMContext::MD_prof:
2509  ++NumProfAttachments;
2510  Assert(NumProfAttachments == 1,
2511  "function must have a single !prof attachment", &F, I.second);
2512  break;
2513  }
2514 
2515  // Verify the metadata itself.
2516  visitMDNode(*I.second, AllowLocs);
2517  }
2518  }
2519 
2520  // If this function is actually an intrinsic, verify that it is only used in
2521  // direct call/invokes, never having its "address taken".
2522  // Only do this if the module is materialized, otherwise we don't have all the
2523  // uses.
2524  if (F.isIntrinsic() && F.getParent()->isMaterialized()) {
2525  const User *U;
2526  if (F.hasAddressTaken(&U))
2527  Assert(false, "Invalid user of intrinsic instruction!", U);
2528  }
2529 
2530  // Check intrinsics' signatures.
2531  switch (F.getIntrinsicID()) {
2532  case Intrinsic::experimental_gc_get_pointer_base: {
2533  FunctionType *FT = F.getFunctionType();
2534  Assert(FT->getNumParams() == 1, "wrong number of parameters", F);
2535  Assert(isa<PointerType>(F.getReturnType()),
2536  "gc.get.pointer.base must return a pointer", F);
2537  Assert(FT->getParamType(0) == F.getReturnType(),
2538  "gc.get.pointer.base operand and result must be of the same type",
2539  F);
2540  break;
2541  }
2542  case Intrinsic::experimental_gc_get_pointer_offset: {
2543  FunctionType *FT = F.getFunctionType();
2544  Assert(FT->getNumParams() == 1, "wrong number of parameters", F);
2545  Assert(isa<PointerType>(FT->getParamType(0)),
2546  "gc.get.pointer.offset operand must be a pointer", F);
2547  Assert(F.getReturnType()->isIntegerTy(),
2548  "gc.get.pointer.offset must return integer", F);
2549  break;
2550  }
2551  }
2552 
2553  auto *N = F.getSubprogram();
2554  HasDebugInfo = (N != nullptr);
2555  if (!HasDebugInfo)
2556  return;
2557 
2558  // Check that all !dbg attachments lead to back to N.
2559  //
2560  // FIXME: Check this incrementally while visiting !dbg attachments.
2561  // FIXME: Only check when N is the canonical subprogram for F.
2563  auto VisitDebugLoc = [&](const Instruction &I, const MDNode *Node) {
2564  // Be careful about using DILocation here since we might be dealing with
2565  // broken code (this is the Verifier after all).
2566  const DILocation *DL = dyn_cast_or_null<DILocation>(Node);
2567  if (!DL)
2568  return;
2569  if (!Seen.insert(DL).second)
2570  return;
2571 
2572  Metadata *Parent = DL->getRawScope();
2573  AssertDI(Parent && isa<DILocalScope>(Parent),
2574  "DILocation's scope must be a DILocalScope", N, &F, &I, DL,
2575  Parent);
2576 
2577  DILocalScope *Scope = DL->getInlinedAtScope();
2578  Assert(Scope, "Failed to find DILocalScope", DL);
2579 
2580  if (!Seen.insert(Scope).second)
2581  return;
2582 
2583  DISubprogram *SP = Scope->getSubprogram();
2584 
2585  // Scope and SP could be the same MDNode and we don't want to skip
2586  // validation in that case
2587  if (SP && ((Scope != SP) && !Seen.insert(SP).second))
2588  return;
2589 
2590  AssertDI(SP->describes(&F),
2591  "!dbg attachment points at wrong subprogram for function", N, &F,
2592  &I, DL, Scope, SP);
2593  };
2594  for (auto &BB : F)
2595  for (auto &I : BB) {
2596  VisitDebugLoc(I, I.getDebugLoc().getAsMDNode());
2597  // The llvm.loop annotations also contain two DILocations.
2598  if (auto MD = I.getMetadata(LLVMContext::MD_loop))
2599  for (unsigned i = 1; i < MD->getNumOperands(); ++i)
2600  VisitDebugLoc(I, dyn_cast_or_null<MDNode>(MD->getOperand(i)));
2601  if (BrokenDebugInfo)
2602  return;
2603  }
2604 }
2605 
2606 // verifyBasicBlock - Verify that a basic block is well formed...
2607 //
2608 void Verifier::visitBasicBlock(BasicBlock &BB) {
2609  InstsInThisBlock.clear();
2610 
2611  // Ensure that basic blocks have terminators!
2612  Assert(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
2613 
2614  // Check constraints that this basic block imposes on all of the PHI nodes in
2615  // it.
2616  if (isa<PHINode>(BB.front())) {
2619  llvm::sort(Preds);
2620  for (const PHINode &PN : BB.phis()) {
2621  Assert(PN.getNumIncomingValues() == Preds.size(),
2622  "PHINode should have one entry for each predecessor of its "
2623  "parent basic block!",
2624  &PN);
2625 
2626  // Get and sort all incoming values in the PHI node...
2627  Values.clear();
2628  Values.reserve(PN.getNumIncomingValues());
2629  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
2630  Values.push_back(
2631  std::make_pair(PN.getIncomingBlock(i), PN.getIncomingValue(i)));
2632  llvm::sort(Values);
2633 
2634  for (unsigned i = 0, e = Values.size(); i != e; ++i) {
2635  // Check to make sure that if there is more than one entry for a
2636  // particular basic block in this PHI node, that the incoming values are
2637  // all identical.
2638  //
2639  Assert(i == 0 || Values[i].first != Values[i - 1].first ||
2640  Values[i].second == Values[i - 1].second,
2641  "PHI node has multiple entries for the same basic block with "
2642  "different incoming values!",
2643  &PN, Values[i].first, Values[i].second, Values[i - 1].second);
2644 
2645  // Check to make sure that the predecessors and PHI node entries are
2646  // matched up.
2647  Assert(Values[i].first == Preds[i],
2648  "PHI node entries do not match predecessors!", &PN,
2649  Values[i].first, Preds[i]);
2650  }
2651  }
2652  }
2653 
2654  // Check that all instructions have their parent pointers set up correctly.
2655  for (auto &I : BB)
2656  {
2657  Assert(I.getParent() == &BB, "Instruction has bogus parent pointer!");
2658  }
2659 }
2660 
2661 void Verifier::visitTerminator(Instruction &I) {
2662  // Ensure that terminators only exist at the end of the basic block.
2663  Assert(&I == I.getParent()->getTerminator(),
2664  "Terminator found in the middle of a basic block!", I.getParent());
2665  visitInstruction(I);
2666 }
2667 
2668 void Verifier::visitBranchInst(BranchInst &BI) {
2669  if (BI.isConditional()) {
2670  Assert(BI.getCondition()->getType()->isIntegerTy(1),
2671  "Branch condition is not 'i1' type!", &BI, BI.getCondition());
2672  }
2673  visitTerminator(BI);
2674 }
2675 
2676 void Verifier::visitReturnInst(ReturnInst &RI) {
2677  Function *F = RI.getParent()->getParent();
2678  unsigned N = RI.getNumOperands();
2679  if (F->getReturnType()->isVoidTy())
2680  Assert(N == 0,
2681  "Found return instr that returns non-void in Function of void "
2682  "return type!",
2683  &RI, F->getReturnType());
2684  else
2685  Assert(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
2686  "Function return type does not match operand "
2687  "type of return inst!",
2688  &RI, F->getReturnType());
2689 
2690  // Check to make sure that the return value has necessary properties for
2691  // terminators...
2692  visitTerminator(RI);
2693 }
2694 
2695 void Verifier::visitSwitchInst(SwitchInst &SI) {
2696  // Check to make sure that all of the constants in the switch instruction
2697  // have the same type as the switched-on value.
2698  Type *SwitchTy = SI.getCondition()->getType();
2700  for (auto &Case : SI.cases()) {
2701  Assert(Case.getCaseValue()->getType() == SwitchTy,
2702  "Switch constants must all be same type as switch value!", &SI);
2703  Assert(Constants.insert(Case.getCaseValue()).second,
2704  "Duplicate integer as switch case", &SI, Case.getCaseValue());
2705  }
2706 
2707  visitTerminator(SI);
2708 }
2709 
2710 void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
2711  Assert(BI.getAddress()->getType()->isPointerTy(),
2712  "Indirectbr operand must have pointer type!", &BI);
2713  for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
2715  "Indirectbr destinations must all have pointer type!", &BI);
2716 
2717  visitTerminator(BI);
2718 }
2719 
2720 void Verifier::visitCallBrInst(CallBrInst &CBI) {
2721  Assert(CBI.isInlineAsm(), "Callbr is currently only used for asm-goto!",
2722  &CBI);
2723  const InlineAsm *IA = cast<InlineAsm>(CBI.getCalledOperand());
2724  Assert(!IA->canThrow(), "Unwinding from Callbr is not allowed");
2725  for (unsigned i = 0, e = CBI.getNumSuccessors(); i != e; ++i)
2726  Assert(CBI.getSuccessor(i)->getType()->isLabelTy(),
2727  "Callbr successors must all have pointer type!", &CBI);
2728  for (unsigned i = 0, e = CBI.getNumOperands(); i != e; ++i) {
2729  Assert(i >= CBI.getNumArgOperands() || !isa<BasicBlock>(CBI.getOperand(i)),
2730  "Using an unescaped label as a callbr argument!", &CBI);
2731  if (isa<BasicBlock>(CBI.getOperand(i)))
2732  for (unsigned j = i + 1; j != e; ++j)
2733  Assert(CBI.getOperand(i) != CBI.getOperand(j),
2734  "Duplicate callbr destination!", &CBI);
2735  }
2736  {
2738  for (Value *V : CBI.args())
2739  if (auto *BA = dyn_cast<BlockAddress>(V))
2740  ArgBBs.insert(BA->getBasicBlock());
2741  for (BasicBlock *BB : CBI.getIndirectDests())
2742  Assert(ArgBBs.count(BB), "Indirect label missing from arglist.", &CBI);
2743  }
2744 
2745  visitTerminator(CBI);
2746 }
2747 
2748 void Verifier::visitSelectInst(SelectInst &SI) {
2749  Assert(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
2750  SI.getOperand(2)),
2751  "Invalid operands for select instruction!", &SI);
2752 
2753  Assert(SI.getTrueValue()->getType() == SI.getType(),
2754  "Select values must have same type as select instruction!", &SI);
2755  visitInstruction(SI);
2756 }
2757 
2758 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
2759 /// a pass, if any exist, it's an error.
2760 ///
2761 void Verifier::visitUserOp1(Instruction &I) {
2762  Assert(false, "User-defined operators should not live outside of a pass!", &I);
2763 }
2764 
2765 void Verifier::visitTruncInst(TruncInst &I) {
2766  // Get the source and destination types
2767  Type *SrcTy = I.getOperand(0)->getType();
2768  Type *DestTy = I.getType();
2769 
2770  // Get the size of the types in bits, we'll need this later
2771  unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2772  unsigned DestBitSize = DestTy->getScalarSizeInBits();
2773 
2774  Assert(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
2775  Assert(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
2776  Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2777  "trunc source and destination must both be a vector or neither", &I);
2778  Assert(SrcBitSize > DestBitSize, "DestTy too big for Trunc", &I);
2779 
2780  visitInstruction(I);
2781 }
2782 
2783 void Verifier::visitZExtInst(ZExtInst &I) {
2784  // Get the source and destination types
2785  Type *SrcTy = I.getOperand(0)->getType();
2786  Type *DestTy = I.getType();
2787 
2788  // Get the size of the types in bits, we'll need this later
2789  Assert(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
2790  Assert(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
2791  Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2792  "zext source and destination must both be a vector or neither", &I);
2793  unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2794  unsigned DestBitSize = DestTy->getScalarSizeInBits();
2795 
2796  Assert(SrcBitSize < DestBitSize, "Type too small for ZExt", &I);
2797 
2798  visitInstruction(I);
2799 }
2800 
2801 void Verifier::visitSExtInst(SExtInst &I) {
2802  // Get the source and destination types
2803  Type *SrcTy = I.getOperand(0)->getType();
2804  Type *DestTy = I.getType();
2805 
2806  // Get the size of the types in bits, we'll need this later
2807  unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2808  unsigned DestBitSize = DestTy->getScalarSizeInBits();
2809 
2810  Assert(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
2811  Assert(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
2812  Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2813  "sext source and destination must both be a vector or neither", &I);
2814  Assert(SrcBitSize < DestBitSize, "Type too small for SExt", &I);
2815 
2816  visitInstruction(I);
2817 }
2818 
2819 void Verifier::visitFPTruncInst(FPTruncInst &I) {
2820  // Get the source and destination types
2821  Type *SrcTy = I.getOperand(0)->getType();
2822  Type *DestTy = I.getType();
2823  // Get the size of the types in bits, we'll need this later
2824  unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2825  unsigned DestBitSize = DestTy->getScalarSizeInBits();
2826 
2827  Assert(SrcTy->isFPOrFPVectorTy(), "FPTrunc only operates on FP", &I);
2828  Assert(DestTy->isFPOrFPVectorTy(), "FPTrunc only produces an FP", &I);
2829  Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2830  "fptrunc source and destination must both be a vector or neither", &I);
2831  Assert(SrcBitSize > DestBitSize, "DestTy too big for FPTrunc", &I);
2832 
2833  visitInstruction(I);
2834 }
2835 
2836 void Verifier::visitFPExtInst(FPExtInst &I) {
2837  // Get the source and destination types
2838  Type *SrcTy = I.getOperand(0)->getType();
2839  Type *DestTy = I.getType();
2840 
2841  // Get the size of the types in bits, we'll need this later
2842  unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2843  unsigned DestBitSize = DestTy->getScalarSizeInBits();
2844 
2845  Assert(SrcTy->isFPOrFPVectorTy(), "FPExt only operates on FP", &I);
2846  Assert(DestTy->isFPOrFPVectorTy(), "FPExt only produces an FP", &I);
2847  Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(),
2848  "fpext source and destination must both be a vector or neither", &I);
2849  Assert(SrcBitSize < DestBitSize, "DestTy too small for FPExt", &I);
2850 
2851  visitInstruction(I);
2852 }
2853 
2854 void Verifier::visitUIToFPInst(UIToFPInst &I) {
2855  // Get the source and destination types
2856  Type *SrcTy = I.getOperand(0)->getType();
2857  Type *DestTy = I.getType();
2858 
2859  bool SrcVec = SrcTy->isVectorTy();
2860  bool DstVec = DestTy->isVectorTy();
2861 
2862  Assert(SrcVec == DstVec,
2863  "UIToFP source and dest must both be vector or scalar", &I);
2864  Assert(SrcTy->isIntOrIntVectorTy(),
2865  "UIToFP source must be integer or integer vector", &I);
2866  Assert(DestTy->isFPOrFPVectorTy(), "UIToFP result must be FP or FP vector",
2867  &I);
2868 
2869  if (SrcVec && DstVec)
2870  Assert(cast<VectorType>(SrcTy)->getElementCount() ==
2871  cast<VectorType>(DestTy)->getElementCount(),
2872  "UIToFP source and dest vector length mismatch", &I);
2873 
2874  visitInstruction(I);
2875 }
2876 
2877 void Verifier::visitSIToFPInst(SIToFPInst &I) {
2878  // Get the source and destination types
2879  Type *SrcTy = I.getOperand(0)->getType();
2880  Type *DestTy = I.getType();
2881 
2882  bool SrcVec = SrcTy->isVectorTy();
2883  bool DstVec = DestTy->isVectorTy();
2884 
2885  Assert(SrcVec == DstVec,
2886  "SIToFP source and dest must both be vector or scalar", &I);
2887  Assert(SrcTy->isIntOrIntVectorTy(),
2888  "SIToFP source must be integer or integer vector", &I);
2889  Assert(DestTy->isFPOrFPVectorTy(), "SIToFP result must be FP or FP vector",
2890  &I);
2891 
2892  if (SrcVec && DstVec)
2893  Assert(cast<VectorType>(SrcTy)->getElementCount() ==
2894  cast<VectorType>(DestTy)->getElementCount(),
2895  "SIToFP source and dest vector length mismatch", &I);
2896 
2897  visitInstruction(I);
2898 }
2899 
2900 void Verifier::visitFPToUIInst(FPToUIInst &I) {
2901  // Get the source and destination types
2902  Type *SrcTy = I.getOperand(0)->getType();
2903  Type *DestTy = I.getType();
2904 
2905  bool SrcVec = SrcTy->isVectorTy();
2906  bool DstVec = DestTy->isVectorTy();
2907 
2908  Assert(SrcVec == DstVec,
2909  "FPToUI source and dest must both be vector or scalar", &I);
2910  Assert(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
2911  &I);
2912  Assert(DestTy->isIntOrIntVectorTy(),
2913  "FPToUI result must be integer or integer vector", &I);
2914 
2915  if (SrcVec && DstVec)
2916  Assert(cast<VectorType>(SrcTy)->getElementCount() ==
2917  cast<VectorType>(DestTy)->getElementCount(),
2918  "FPToUI source and dest vector length mismatch", &I);
2919 
2920  visitInstruction(I);
2921 }
2922 
2923 void Verifier::visitFPToSIInst(FPToSIInst &I) {
2924  // Get the source and destination types
2925  Type *SrcTy = I.getOperand(0)->getType();
2926  Type *DestTy = I.getType();
2927 
2928  bool SrcVec = SrcTy->isVectorTy();
2929  bool DstVec = DestTy->isVectorTy();
2930 
2931  Assert(SrcVec == DstVec,
2932  "FPToSI source and dest must both be vector or scalar", &I);
2933  Assert(SrcTy->isFPOrFPVectorTy(), "FPToSI source must be FP or FP vector",
2934  &I);
2935  Assert(DestTy->isIntOrIntVectorTy(),
2936  "FPToSI result must be integer or integer vector", &I);
2937 
2938  if (SrcVec && DstVec)
2939  Assert(cast<VectorType>(SrcTy)->getElementCount() ==
2940  cast<VectorType>(DestTy)->getElementCount(),
2941  "FPToSI source and dest vector length mismatch", &I);
2942 
2943  visitInstruction(I);
2944 }
2945 
2946 void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
2947  // Get the source and destination types
2948  Type *SrcTy = I.getOperand(0)->getType();
2949  Type *DestTy = I.getType();
2950 
2951  Assert(SrcTy->isPtrOrPtrVectorTy(), "PtrToInt source must be pointer", &I);
2952 
2953  Assert(DestTy->isIntOrIntVectorTy(), "PtrToInt result must be integral", &I);
2954  Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "PtrToInt type mismatch",
2955  &I);
2956 
2957  if (SrcTy->isVectorTy()) {
2958  auto *VSrc = cast<VectorType>(SrcTy);
2959  auto *VDest = cast<VectorType>(DestTy);
2960  Assert(VSrc->getElementCount() == VDest->getElementCount(),
2961  "PtrToInt Vector width mismatch", &I);
2962  }
2963 
2964  visitInstruction(I);
2965 }
2966 
2967 void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
2968  // Get the source and destination types
2969  Type *SrcTy = I.getOperand(0)->getType();
2970  Type *DestTy = I.getType();
2971 
2972  Assert(SrcTy->isIntOrIntVectorTy(),
2973  "IntToPtr source must be an integral", &I);
2974  Assert(DestTy->isPtrOrPtrVectorTy(), "IntToPtr result must be a pointer", &I);
2975 
2976  Assert(SrcTy->isVectorTy() == DestTy->isVectorTy(), "IntToPtr type mismatch",
2977  &I);
2978  if (SrcTy->isVectorTy()) {
2979  auto *VSrc = cast<VectorType>(SrcTy);
2980  auto *VDest = cast<VectorType>(DestTy);
2981  Assert(VSrc->getElementCount() == VDest->getElementCount(),
2982  "IntToPtr Vector width mismatch", &I);
2983  }
2984  visitInstruction(I);
2985 }
2986 
2987 void Verifier::visitBitCastInst(BitCastInst &I) {
2988  Assert(
2989  CastInst::castIsValid(Instruction::BitCast, I.getOperand(0), I.getType()),
2990  "Invalid bitcast", &I);
2991  visitInstruction(I);
2992 }
2993 
2994 void Verifier::visitAddrSpaceCastInst(AddrSpaceCastInst &I) {
2995  Type *SrcTy = I.getOperand(0)->getType();
2996  Type *DestTy = I.getType();
2997 
2998  Assert(SrcTy->isPtrOrPtrVectorTy(), "AddrSpaceCast source must be a pointer",
2999  &I);
3000  Assert(DestTy->isPtrOrPtrVectorTy(), "AddrSpaceCast result must be a pointer",
3001  &I);
3003  "AddrSpaceCast must be between different address spaces", &I);
3004  if (auto *SrcVTy = dyn_cast<VectorType>(SrcTy))
3005  Assert(SrcVTy->getElementCount() ==
3006  cast<VectorType>(DestTy)->getElementCount(),
3007  "AddrSpaceCast vector pointer number of elements mismatch", &I);
3008  visitInstruction(I);
3009 }
3010 
3011 /// visitPHINode - Ensure that a PHI node is well formed.
3012 ///
3013 void Verifier::visitPHINode(PHINode &PN) {
3014  // Ensure that the PHI nodes are all grouped together at the top of the block.
3015  // This can be tested by checking whether the instruction before this is
3016  // either nonexistent (because this is begin()) or is a PHI node. If not,
3017  // then there is some other instruction before a PHI.
3018  Assert(&PN == &PN.getParent()->front() ||
3019  isa<PHINode>(--BasicBlock::iterator(&PN)),
3020  "PHI nodes not grouped at top of basic block!", &PN, PN.getParent());
3021 
3022  // Check that a PHI doesn't yield a Token.
3023  Assert(!PN.getType()->isTokenTy(), "PHI nodes cannot have token type!");
3024 
3025  // Check that all of the values of the PHI node have the same type as the
3026  // result, and that the incoming blocks are really basic blocks.
3027  for (Value *IncValue : PN.incoming_values()) {
3028  Assert(PN.getType() == IncValue->getType(),
3029  "PHI node operands are not the same type as the result!", &PN);
3030  }
3031 
3032  // All other PHI node constraints are checked in the visitBasicBlock method.
3033 
3034  visitInstruction(PN);
3035 }
3036 
3037 void Verifier::visitCallBase(CallBase &Call) {
3038  Assert(Call.getCalledOperand()->getType()->isPointerTy(),
3039  "Called function must be a pointer!", Call);
3040  PointerType *FPTy = cast<PointerType>(Call.getCalledOperand()->getType());
3041 
3042  Assert(FPTy->isOpaqueOrPointeeTypeMatches(Call.getFunctionType()),
3043  "Called function is not the same type as the call!", Call);
3044 
3045  FunctionType *FTy = Call.getFunctionType();
3046 
3047  // Verify that the correct number of arguments are being passed
3048  if (FTy->isVarArg())
3049  Assert(Call.arg_size() >= FTy->getNumParams(),
3050  "Called function requires more parameters than were provided!",
3051  Call);
3052  else
3053  Assert(Call.arg_size() == FTy->getNumParams(),
3054  "Incorrect number of arguments passed to called function!", Call);
3055 
3056  // Verify that all arguments to the call match the function type.
3057  for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
3058  Assert(Call.getArgOperand(i)->getType() == FTy->getParamType(i),
3059  "Call parameter type does not match function signature!",
3060  Call.getArgOperand(i), FTy->getParamType(i), Call);
3061 
3062  AttributeList Attrs = Call.getAttributes();
3063 
3064  Assert(verifyAttributeCount(Attrs, Call.arg_size()),
3065  "Attribute after last parameter!", Call);
3066 
3067  Function *Callee =
3068  dyn_cast<Function>(Call.getCalledOperand()->stripPointerCasts());
3069  bool IsIntrinsic = Callee && Callee->isIntrinsic();
3070  if (IsIntrinsic)
3071  Assert(Callee->getValueType() == FTy,
3072  "Intrinsic called with incompatible signature", Call);
3073 
3074  if (Attrs.hasFnAttribute(Attribute::Speculatable)) {
3075  // Don't allow speculatable on call sites, unless the underlying function
3076  // declaration is also speculatable.
3077  Assert(Callee && Callee->isSpeculatable(),
3078  "speculatable attribute may not apply to call sites", Call);
3079  }
3080 
3081  if (Attrs.hasFnAttribute(Attribute::Preallocated)) {
3082  Assert(Call.getCalledFunction()->getIntrinsicID() ==
3083  Intrinsic::call_preallocated_arg,
3084  "preallocated as a call site attribute can only be on "
3085  "llvm.call.preallocated.arg");
3086  }
3087 
3088  // Verify call attributes.
3089  verifyFunctionAttrs(FTy, Attrs, &Call, IsIntrinsic);
3090 
3091  // Conservatively check the inalloca argument.
3092  // We have a bug if we can find that there is an underlying alloca without
3093  // inalloca.
3094  if (Call.hasInAllocaArgument()) {
3095  Value *InAllocaArg = Call.getArgOperand(FTy->getNumParams() - 1);
3096  if (auto AI = dyn_cast<AllocaInst>(InAllocaArg->stripInBoundsOffsets()))
3097  Assert(AI->isUsedWithInAlloca(),
3098  "inalloca argument for call has mismatched alloca", AI, Call);
3099  }
3100 
3101  // For each argument of the callsite, if it has the swifterror argument,
3102  // make sure the underlying alloca/parameter it comes from has a swifterror as
3103  // well.
3104  for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
3105  if (Call.paramHasAttr(i, Attribute::SwiftError)) {
3106  Value *SwiftErrorArg = Call.getArgOperand(i);
3107  if (auto AI = dyn_cast<AllocaInst>(SwiftErrorArg->stripInBoundsOffsets())) {
3108  Assert(AI->isSwiftError(),
3109  "swifterror argument for call has mismatched alloca", AI, Call);
3110  continue;
3111  }
3112  auto ArgI = dyn_cast<Argument>(SwiftErrorArg);
3113  Assert(ArgI,
3114  "swifterror argument should come from an alloca or parameter",
3115  SwiftErrorArg, Call);
3116  Assert(ArgI->hasSwiftErrorAttr(),
3117  "swifterror argument for call has mismatched parameter", ArgI,
3118  Call);
3119  }
3120 
3121  if (Attrs.hasParamAttribute(i, Attribute::ImmArg)) {
3122  // Don't allow immarg on call sites, unless the underlying declaration
3123  // also has the matching immarg.
3124  Assert(Callee && Callee->hasParamAttribute(i, Attribute::ImmArg),
3125  "immarg may not apply only to call sites",
3126  Call.getArgOperand(i), Call);
3127  }
3128 
3129  if (Call.paramHasAttr(i, Attribute::ImmArg)) {
3130  Value *ArgVal = Call.getArgOperand(i);
3131  Assert(isa<ConstantInt>(ArgVal) || isa<ConstantFP>(ArgVal),
3132  "immarg operand has non-immediate parameter", ArgVal, Call);
3133  }
3134 
3135  if (Call.paramHasAttr(i, Attribute::Preallocated)) {
3136  Value *ArgVal = Call.getArgOperand(i);
3137  bool hasOB =
3138  Call.countOperandBundlesOfType(LLVMContext::OB_preallocated) != 0;
3139  bool isMustTail = Call.isMustTailCall();
3140  Assert(hasOB != isMustTail,
3141  "preallocated operand either requires a preallocated bundle or "
3142  "the call to be musttail (but not both)",
3143  ArgVal, Call);
3144  }
3145  }
3146 
3147  if (FTy->isVarArg()) {
3148  // FIXME? is 'nest' even legal here?
3149  bool SawNest = false;
3150  bool SawReturned = false;
3151 
3152  for (unsigned Idx = 0; Idx < FTy->getNumParams(); ++Idx) {
3153  if (Attrs.hasParamAttribute(Idx, Attribute::Nest))
3154  SawNest = true;
3155  if (Attrs.hasParamAttribute(Idx, Attribute::Returned))
3156  SawReturned = true;
3157  }
3158 
3159  // Check attributes on the varargs part.
3160  for (unsigned Idx = FTy->getNumParams(); Idx < Call.arg_size(); ++Idx) {
3161  Type *Ty = Call.getArgOperand(Idx)->getType();
3162  AttributeSet ArgAttrs = Attrs.getParamAttributes(Idx);
3163  verifyParameterAttrs(ArgAttrs, Ty, &Call);
3164 
3165  if (ArgAttrs.hasAttribute(Attribute::Nest)) {
3166  Assert(!SawNest, "More than one parameter has attribute nest!", Call);
3167  SawNest = true;
3168  }
3169 
3170  if (ArgAttrs.hasAttribute(Attribute::Returned)) {
3171  Assert(!SawReturned, "More than one parameter has attribute returned!",
3172  Call);
3174  "Incompatible argument and return types for 'returned' "
3175  "attribute",
3176  Call);
3177  SawReturned = true;
3178  }
3179 
3180  // Statepoint intrinsic is vararg but the wrapped function may be not.
3181  // Allow sret here and check the wrapped function in verifyStatepoint.
3182  if (!Call.getCalledFunction() ||
3183  Call.getCalledFunction()->getIntrinsicID() !=
3184  Intrinsic::experimental_gc_statepoint)
3185  Assert(!ArgAttrs.hasAttribute(Attribute::StructRet),
3186  "Attribute 'sret' cannot be used for vararg call arguments!",
3187  Call);
3188 
3189  if (ArgAttrs.hasAttribute(Attribute::InAlloca))
3190  Assert(Idx == Call.arg_size() - 1,
3191  "inalloca isn't on the last argument!", Call);
3192  }
3193  }
3194 
3195  // Verify that there's no metadata unless it's a direct call to an intrinsic.
3196  if (!IsIntrinsic) {
3197  for (Type *ParamTy : FTy->params()) {
3198  Assert(!ParamTy->isMetadataTy(),
3199  "Function has metadata parameter but isn't an intrinsic", Call);
3200  Assert(!ParamTy->isTokenTy(),
3201  "Function has token parameter but isn't an intrinsic", Call);
3202  }
3203  }
3204 
3205  // Verify that indirect calls don't return tokens.
3206  if (!Call.getCalledFunction()) {
3207  Assert(!FTy->getReturnType()->isTokenTy(),
3208  "Return type cannot be token for indirect call!");
3209  Assert(!FTy->getReturnType()->isX86_AMXTy(),
3210  "Return type cannot be x86_amx for indirect call!");
3211  }
3212 
3213  if (Function *F = Call.getCalledFunction())
3214  if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
3215  visitIntrinsicCall(ID, Call);
3216 
3217  // Verify that a callsite has at most one "deopt", at most one "funclet", at
3218  // most one "gc-transition", at most one "cfguardtarget",
3219  // and at most one "preallocated" operand bundle.
3220  bool FoundDeoptBundle = false, FoundFuncletBundle = false,
3221  FoundGCTransitionBundle = false, FoundCFGuardTargetBundle = false,
3222  FoundPreallocatedBundle = false, FoundGCLiveBundle = false,
3223  FoundAttachedCallBundle = false;
3224  for (unsigned i = 0, e = Call.getNumOperandBundles(); i < e; ++i) {
3225  OperandBundleUse BU = Call.getOperandBundleAt(i);
3226  uint32_t Tag = BU.getTagID();
3227  if (Tag == LLVMContext::OB_deopt) {
3228  Assert(!FoundDeoptBundle, "Multiple deopt operand bundles", Call);
3229  FoundDeoptBundle = true;
3230  } else if (Tag == LLVMContext::OB_gc_transition) {
3231  Assert(!FoundGCTransitionBundle, "Multiple gc-transition operand bundles",
3232  Call);
3233  FoundGCTransitionBundle = true;
3234  } else if (Tag == LLVMContext::OB_funclet) {
3235  Assert(!FoundFuncletBundle, "Multiple funclet operand bundles", Call);
3236  FoundFuncletBundle = true;
3237  Assert(BU.Inputs.size() == 1,
3238  "Expected exactly one funclet bundle operand", Call);
3239  Assert(isa<FuncletPadInst>(BU.Inputs.front()),
3240  "Funclet bundle operands should correspond to a FuncletPadInst",
3241  Call);
3242  } else if (Tag == LLVMContext::OB_cfguardtarget) {
3243  Assert(!FoundCFGuardTargetBundle,
3244  "Multiple CFGuardTarget operand bundles", Call);
3245  FoundCFGuardTargetBundle = true;
3246  Assert(BU.Inputs.size() == 1,
3247  "Expected exactly one cfguardtarget bundle operand", Call);
3248  } else if (Tag == LLVMContext::OB_preallocated) {
3249  Assert(!FoundPreallocatedBundle, "Multiple preallocated operand bundles",
3250  Call);
3251  FoundPreallocatedBundle = true;
3252  Assert(BU.Inputs.size() == 1,
3253  "Expected exactly one preallocated bundle operand", Call);
3254  auto Input = dyn_cast<IntrinsicInst>(BU.Inputs.front());
3255  Assert(Input &&
3256  Input->getIntrinsicID() == Intrinsic::call_preallocated_setup,
3257  "\"preallocated\" argument must be a token from "
3258  "llvm.call.preallocated.setup",
3259  Call);
3260  } else if (Tag == LLVMContext::OB_gc_live) {
3261  Assert(!FoundGCLiveBundle, "Multiple gc-live operand bundles",
3262  Call);
3263  FoundGCLiveBundle = true;
3264  } else if (Tag == LLVMContext::OB_clang_arc_attachedcall) {
3265  Assert(!FoundAttachedCallBundle,
3266  "Multiple \"clang.arc.attachedcall\" operand bundles", Call);
3267  FoundAttachedCallBundle = true;
3268  }
3269  }
3270 
3271  if (FoundAttachedCallBundle)
3272  Assert((FTy->getReturnType()->isPointerTy() ||
3273  (Call.doesNotReturn() && FTy->getReturnType()->isVoidTy())),
3274  "a call with operand bundle \"clang.arc.attachedcall\" must call a "
3275  "function returning a pointer or a non-returning function that has "
3276  "a void return type",
3277  Call);
3278 
3279  // Verify that each inlinable callsite of a debug-info-bearing function in a
3280  // debug-info-bearing function has a debug location attached to it. Failure to
3281  // do so causes assertion failures when the inliner sets up inline scope info.
3282  if (Call.getFunction()->getSubprogram() && Call.getCalledFunction() &&
3283  Call.getCalledFunction()->getSubprogram())
3284  AssertDI(Call.getDebugLoc(),
3285  "inlinable function call in a function with "
3286  "debug info must have a !dbg location",
3287  Call);
3288 
3289  visitInstruction(Call);
3290 }
3291 
3292 void Verifier::verifyTailCCMustTailAttrs(AttrBuilder Attrs,
3293  StringRef Context) {
3294  Assert(!Attrs.contains(Attribute::InAlloca),
3295  Twine("inalloca attribute not allowed in ") + Context);
3296  Assert(!Attrs.contains(Attribute::InReg),
3297  Twine("inreg attribute not allowed in ") + Context);
3298  Assert(!Attrs.contains(Attribute::SwiftError),
3299  Twine("swifterror attribute not allowed in ") + Context);
3300  Assert(!Attrs.contains(Attribute::Preallocated),
3301  Twine("preallocated attribute not allowed in ") + Context);
3302  Assert(!Attrs.contains(Attribute::ByRef),
3303  Twine("byref attribute not allowed in ") + Context);
3304 }
3305 
3306 /// Two types are "congruent" if they are identical, or if they are both pointer
3307 /// types with different pointee types and the same address space.
3308 static bool isTypeCongruent(Type *L, Type *R) {
3309  if (L == R)
3310  return true;
3311  PointerType *PL = dyn_cast<PointerType>(L);
3312  PointerType *PR = dyn_cast<PointerType>(R);
3313  if (!PL || !PR)
3314  return false;
3315  return PL->getAddressSpace() == PR->getAddressSpace();
3316 }
3317 
3319  static const Attribute::AttrKind ABIAttrs[] = {
3320  Attribute::StructRet, Attribute::ByVal, Attribute::InAlloca,
3321  Attribute::InReg, Attribute::StackAlignment, Attribute::SwiftSelf,
3322  Attribute::SwiftAsync, Attribute::SwiftError, Attribute::Preallocated,
3323  Attribute::ByRef};
3324  AttrBuilder Copy;
3325  for (auto AK : ABIAttrs) {
3326  Attribute Attr = Attrs.getParamAttributes(I).getAttribute(AK);
3327  if (Attr.isValid())
3328  Copy.addAttribute(Attr);
3329  }
3330 
3331  // `align` is ABI-affecting only in combination with `byval` or `byref`.
3332  if (Attrs.hasParamAttribute(I, Attribute::Alignment) &&
3333  (Attrs.hasParamAttribute(I, Attribute::ByVal) ||
3334  Attrs.hasParamAttribute(I, Attribute::ByRef)))
3335  Copy.addAlignmentAttr(Attrs.getParamAlignment(I));
3336  return Copy;
3337 }
3338 
3339 void Verifier::verifyMustTailCall(CallInst &CI) {
3340  Assert(!CI.isInlineAsm(), "cannot use musttail call with inline asm", &CI);
3341 
3342  Function *F = CI.getParent()->getParent();
3343  FunctionType *CallerTy = F->getFunctionType();
3344  FunctionType *CalleeTy = CI.getFunctionType();
3345  Assert(CallerTy->isVarArg() == CalleeTy->isVarArg(),
3346  "cannot guarantee tail call due to mismatched varargs", &CI);
3347  Assert(isTypeCongruent(CallerTy->getReturnType(), CalleeTy->getReturnType()),
3348  "cannot guarantee tail call due to mismatched return types", &CI);
3349 
3350  // - The calling conventions of the caller and callee must match.
3351  Assert(F->getCallingConv() == CI.getCallingConv(),
3352  "cannot guarantee tail call due to mismatched calling conv", &CI);
3353 
3354  // - The call must immediately precede a :ref:`ret <i_ret>` instruction,
3355  // or a pointer bitcast followed by a ret instruction.
3356  // - The ret instruction must return the (possibly bitcasted) value
3357  // produced by the call or void.
3358  Value *RetVal = &CI;
3359  Instruction *Next = CI.getNextNode();
3360 
3361  // Handle the optional bitcast.
3362  if (BitCastInst *BI = dyn_cast_or_null<BitCastInst>(Next)) {
3363  Assert(BI->getOperand(0) == RetVal,
3364  "bitcast following musttail call must use the call", BI);
3365  RetVal = BI;
3366  Next = BI->getNextNode();
3367  }
3368 
3369  // Check the return.
3370  ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
3371  Assert(Ret, "musttail call must precede a ret with an optional bitcast",
3372  &CI);
3373  Assert(!Ret->getReturnValue() || Ret->getReturnValue() == RetVal ||
3374  isa<UndefValue>(Ret->getReturnValue()),
3375  "musttail call result must be returned", Ret);
3376 
3377  AttributeList CallerAttrs = F->getAttributes();
3378  AttributeList CalleeAttrs = CI.getAttributes();
3379  if (CI.getCallingConv() == CallingConv::SwiftTail ||
3381  StringRef CCName =
3382  CI.getCallingConv() == CallingConv::Tail ? "tailcc" : "swifttailcc";
3383 
3384  // - Only sret, byval, swiftself, and swiftasync ABI-impacting attributes
3385  // are allowed in swifttailcc call
3386  for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3387  AttrBuilder ABIAttrs = getParameterABIAttributes(I, CallerAttrs);
3388  SmallString<32> Context{CCName, StringRef(" musttail caller")};
3389  verifyTailCCMustTailAttrs(ABIAttrs, Context);
3390  }
3391  for (int I = 0, E = CalleeTy->getNumParams(); I != E; ++I) {
3392  AttrBuilder ABIAttrs = getParameterABIAttributes(I, CalleeAttrs);
3393  SmallString<32> Context{CCName, StringRef(" musttail callee")};
3394  verifyTailCCMustTailAttrs(ABIAttrs, Context);
3395  }
3396  // - Varargs functions are not allowed
3397  Assert(!CallerTy->isVarArg(), Twine("cannot guarantee ") + CCName +
3398  " tail call for varargs function");
3399  return;
3400  }
3401 
3402  // - The caller and callee prototypes must match. Pointer types of
3403  // parameters or return types may differ in pointee type, but not
3404  // address space.
3405  if (!CI.getCalledFunction() || !CI.getCalledFunction()->isIntrinsic()) {
3406  Assert(CallerTy->getNumParams() == CalleeTy->getNumParams(),
3407  "cannot guarantee tail call due to mismatched parameter counts",
3408  &CI);
3409  for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3410  Assert(
3411  isTypeCongruent(CallerTy->getParamType(I), CalleeTy->getParamType(I)),
3412  "cannot guarantee tail call due to mismatched parameter types", &CI);
3413  }
3414  }
3415 
3416  // - All ABI-impacting function attributes, such as sret, byval, inreg,
3417  // returned, preallocated, and inalloca, must match.
3418  for (int I = 0, E = CallerTy->getNumParams(); I != E; ++I) {
3419  AttrBuilder CallerABIAttrs = getParameterABIAttributes(I, CallerAttrs);
3420  AttrBuilder CalleeABIAttrs = getParameterABIAttributes(I, CalleeAttrs);
3421  Assert(CallerABIAttrs == CalleeABIAttrs,
3422  "cannot guarantee tail call due to mismatched ABI impacting "
3423  "function attributes",
3424  &CI, CI.getOperand(I));
3425  }
3426 }
3427 
3428 void Verifier::visitCallInst(CallInst &CI) {
3429  visitCallBase(CI);
3430 
3431  if (CI.isMustTailCall())
3432  verifyMustTailCall(CI);
3433 }
3434 
3435 void Verifier::visitInvokeInst(InvokeInst &II) {
3436  visitCallBase(II);
3437 
3438  // Verify that the first non-PHI instruction of the unwind destination is an
3439  // exception handling instruction.
3440  Assert(
3441  II.getUnwindDest()->isEHPad(),
3442  "The unwind destination does not have an exception handling instruction!",
3443  &II);
3444 
3445  visitTerminator(II);
3446 }
3447 
3448 /// visitUnaryOperator - Check the argument to the unary operator.
3449 ///
3450 void Verifier::visitUnaryOperator(UnaryOperator &U) {
3451  Assert(U.getType() == U.getOperand(0)->getType(),
3452  "Unary operators must have same type for"
3453  "operands and result!",
3454  &U);
3455 
3456  switch (U.getOpcode()) {
3457  // Check that floating-point arithmetic operators are only used with
3458  // floating-point operands.
3459  case Instruction::FNeg:
3461  "FNeg operator only works with float types!", &U);
3462  break;
3463  default:
3464  llvm_unreachable("Unknown UnaryOperator opcode!");
3465  }
3466 
3467  visitInstruction(U);
3468 }
3469 
3470 /// visitBinaryOperator - Check that both arguments to the binary operator are
3471 /// of the same type!
3472 ///
3473 void Verifier::visitBinaryOperator(BinaryOperator &B) {
3474  Assert(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
3475  "Both operands to a binary operator are not of the same type!", &B);
3476 
3477  switch (B.getOpcode()) {
3478  // Check that integer arithmetic operators are only used with
3479  // integral operands.
3480  case Instruction::Add:
3481  case Instruction::Sub:
3482  case Instruction::Mul:
3483  case Instruction::SDiv:
3484  case Instruction::UDiv:
3485  case Instruction::SRem:
3486  case Instruction::URem:
3487  Assert(B.getType()->isIntOrIntVectorTy(),
3488  "Integer arithmetic operators only work with integral types!", &B);
3489  Assert(B.getType() == B.getOperand(0)->getType(),
3490  "Integer arithmetic operators must have same type "
3491  "for operands and result!",
3492  &B);
3493  break;
3494  // Check that floating-point arithmetic operators are only used with
3495  // floating-point operands.
3496  case Instruction::FAdd:
3497  case Instruction::FSub:
3498  case Instruction::FMul:
3499  case Instruction::FDiv:
3500  case Instruction::FRem:
3501  Assert(B.getType()->isFPOrFPVectorTy(),
3502  "Floating-point arithmetic operators only work with "
3503  "floating-point types!",
3504  &B);
3505  Assert(B.getType() == B.getOperand(0)->getType(),
3506  "Floating-point arithmetic operators must have same type "
3507  "for operands and result!",
3508  &B);
3509  break;
3510  // Check that logical operators are only used with integral operands.
3511  case Instruction::And:
3512  case Instruction::Or:
3513  case Instruction::Xor:
3514  Assert(B.getType()->isIntOrIntVectorTy(),
3515  "Logical operators only work with integral types!", &B);
3516  Assert(B.getType() == B.getOperand(0)->getType(),
3517  "Logical operators must have same type for operands and result!",
3518  &B);
3519  break;
3520  case Instruction::Shl:
3521  case Instruction::LShr:
3522  case Instruction::AShr:
3523  Assert(B.getType()->isIntOrIntVectorTy(),
3524  "Shifts only work with integral types!", &B);
3525  Assert(B.getType() == B.getOperand(0)->getType(),
3526  "Shift return type must be same as operands!", &B);
3527  break;
3528  default:
3529  llvm_unreachable("Unknown BinaryOperator opcode!");
3530  }
3531 
3532  visitInstruction(B);
3533 }
3534 
3535 void Verifier::visitICmpInst(ICmpInst &IC) {
3536  // Check that the operands are the same type
3537  Type *Op0Ty = IC.getOperand(0)->getType();
3538  Type *Op1Ty = IC.getOperand(1)->getType();
3539  Assert(Op0Ty == Op1Ty,
3540  "Both operands to ICmp instruction are not of the same type!", &IC);
3541  // Check that the operands are the right type
3542  Assert(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPtrOrPtrVectorTy(),
3543  "Invalid operand types for ICmp instruction", &IC);
3544  // Check that the predicate is valid.
3545  Assert(IC.isIntPredicate(),
3546  "Invalid predicate in ICmp instruction!", &IC);
3547 
3548  visitInstruction(IC);
3549 }
3550 
3551 void Verifier::visitFCmpInst(FCmpInst &FC) {
3552  // Check that the operands are the same type
3553  Type *Op0Ty = FC.getOperand(0)->getType();
3554  Type *Op1Ty = FC.getOperand(1)->getType();
3555  Assert(Op0Ty == Op1Ty,
3556  "Both operands to FCmp instruction are not of the same type!", &FC);
3557  // Check that the operands are the right type
3558  Assert(Op0Ty->isFPOrFPVectorTy(),
3559  "Invalid operand types for FCmp instruction", &FC);
3560  // Check that the predicate is valid.
3561  Assert(FC.isFPPredicate(),
3562  "Invalid predicate in FCmp instruction!", &FC);
3563 
3564  visitInstruction(FC);
3565 }
3566 
3567 void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
3568  Assert(
3570  "Invalid extractelement operands!", &EI);
3571  visitInstruction(EI);
3572 }
3573 
3574 void Verifier::visitInsertElementInst(InsertElementInst &IE) {
3575  Assert(InsertElementInst::isValidOperands(IE.getOperand(0), IE.getOperand(1),
3576  IE.getOperand(2)),
3577  "Invalid insertelement operands!", &IE);
3578  visitInstruction(IE);
3579 }
3580 
3581 void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
3583  SV.getShuffleMask()),
3584  "Invalid shufflevector operands!", &SV);
3585  visitInstruction(SV);
3586 }
3587 
3588 void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
3589  Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
3590 
3591  Assert(isa<PointerType>(TargetTy),
3592  "GEP base pointer is not a vector or a vector of pointers", &GEP);
3593  Assert(GEP.getSourceElementType()->isSized(), "GEP into unsized type!", &GEP);
3594 
3595  SmallVector<Value *, 16> Idxs(GEP.indices());
3596  Assert(all_of(
3597  Idxs, [](Value* V) { return V->getType()->isIntOrIntVectorTy(); }),
3598  "GEP indexes must be integers", &GEP);
3599  Type *ElTy =
3600  GetElementPtrInst::getIndexedType(GEP.getSourceElementType(), Idxs);
3601  Assert(ElTy, "Invalid indices for GEP pointer type!", &GEP);
3602 
3603  Assert(GEP.getType()->isPtrOrPtrVectorTy() &&
3604  GEP.getResultElementType() == ElTy,
3605  "GEP is not of right type for indices!", &GEP, ElTy);
3606 
3607  if (auto *GEPVTy = dyn_cast<VectorType>(GEP.getType())) {
3608  // Additional checks for vector GEPs.
3609  ElementCount GEPWidth = GEPVTy->getElementCount();
3610  if (GEP.getPointerOperandType()->isVectorTy())
3611  Assert(
3612  GEPWidth ==
3613  cast<VectorType>(GEP.getPointerOperandType())->getElementCount(),
3614  "Vector GEP result width doesn't match operand's", &GEP);
3615  for (Value *Idx : Idxs) {
3616  Type *IndexTy = Idx->getType();
3617  if (auto *IndexVTy = dyn_cast<VectorType>(IndexTy)) {
3618  ElementCount IndexWidth = IndexVTy->getElementCount();
3619  Assert(IndexWidth == GEPWidth, "Invalid GEP index vector width", &GEP);
3620  }
3621  Assert(IndexTy->isIntOrIntVectorTy(),
3622  "All GEP indices should be of integer type");
3623  }
3624  }
3625 
3626  if (auto *PTy = dyn_cast<PointerType>(GEP.getType())) {
3627  Assert(GEP.getAddressSpace() == PTy->getAddressSpace(),
3628  "GEP address space doesn't match type", &GEP);
3629  }
3630 
3631  visitInstruction(GEP);
3632 }
3633 
3634 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
3635  return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
3636 }
3637 
3638 void Verifier::visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty) {
3639  assert(Range && Range == I.getMetadata(LLVMContext::MD_range) &&
3640  "precondition violation");
3641 
3642  unsigned NumOperands = Range->getNumOperands();
3643  Assert(NumOperands % 2 == 0, "Unfinished range!", Range);
3644  unsigned NumRanges = NumOperands / 2;
3645  Assert(NumRanges >= 1, "It should have at least one range!", Range);
3646 
3647  ConstantRange LastRange(1, true); // Dummy initial value
3648  for (unsigned i = 0; i < NumRanges; ++i) {
3649  ConstantInt *Low =
3650  mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
3651  Assert(Low, "The lower limit must be an integer!", Low);
3652  ConstantInt *High =
3653  mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
3654  Assert(High, "The upper limit must be an integer!", High);
3655  Assert(High->getType() == Low->getType() && High->getType() == Ty,
3656  "Range types must match instruction type!", &I);
3657 
3658  APInt HighV = High->getValue();
3659  APInt LowV = Low->getValue();
3660  ConstantRange CurRange(LowV, HighV);
3661  Assert(!CurRange.isEmptySet() && !CurRange.isFullSet(),
3662  "Range must not be empty!", Range);
3663  if (i != 0) {
3664  Assert(CurRange.intersectWith(LastRange).isEmptySet(),
3665  "Intervals are overlapping", Range);
3666  Assert(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
3667  Range);
3668  Assert(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
3669  Range);
3670  }
3671  LastRange = ConstantRange(LowV, HighV);
3672  }
3673  if (NumRanges > 2) {
3674  APInt FirstLow =
3675  mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
3676  APInt FirstHigh =
3677  mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
3678  ConstantRange FirstRange(FirstLow, FirstHigh);
3679  Assert(FirstRange.intersectWith(LastRange).isEmptySet(),
3680  "Intervals are overlapping", Range);
3681  Assert(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
3682  Range);
3683  }
3684 }
3685 
3686 void Verifier::checkAtomicMemAccessSize(Type *Ty, const Instruction *I) {
3687  unsigned Size = DL.getTypeSizeInBits(Ty);
3688  Assert(Size >= 8, "atomic memory access' size must be byte-sized", Ty, I);
3689  Assert(!(Size & (Size - 1)),
3690  "atomic memory access' operand must have a power-of-two size", Ty, I);
3691 }
3692 
3693 void Verifier::visitLoadInst(LoadInst &LI) {
3694  PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
3695  Assert(PTy, "Load operand must be a pointer.", &LI);
3696  Type *ElTy = LI.getType();
3698  "huge alignment values are unsupported", &LI);
3699  Assert(ElTy->isSized(), "loading unsized types is not allowed", &LI);
3700  if (LI.isAtomic()) {
3703  "Load cannot have Release ordering", &LI);
3704  Assert(LI.getAlignment() != 0,
3705  "Atomic load must specify explicit alignment", &LI);
3706  Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3707  "atomic load operand must have integer, pointer, or floating point "
3708  "type!",
3709  ElTy, &LI);
3710  checkAtomicMemAccessSize(ElTy, &LI);
3711  } else {
3713  "Non-atomic load cannot have SynchronizationScope specified", &LI);
3714  }
3715 
3716  visitInstruction(LI);
3717 }
3718 
3719 void Verifier::visitStoreInst(StoreInst &SI) {
3720  PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
3721  Assert(PTy, "Store operand must be a pointer.", &SI);
3722  Type *ElTy = SI.getOperand(0)->getType();
3724  "Stored value type does not match pointer operand type!", &SI, ElTy);
3725  Assert(SI.getAlignment() <= Value::MaximumAlignment,
3726  "huge alignment values are unsupported", &SI);
3727  Assert(ElTy->isSized(), "storing unsized types is not allowed", &SI);
3728  if (SI.isAtomic()) {
3729  Assert(SI.getOrdering() != AtomicOrdering::Acquire &&
3730  SI.getOrdering() != AtomicOrdering::AcquireRelease,
3731  "Store cannot have Acquire ordering", &SI);
3732  Assert(SI.getAlignment() != 0,
3733  "Atomic store must specify explicit alignment", &SI);
3734  Assert(ElTy->isIntOrPtrTy() || ElTy->isFloatingPointTy(),
3735  "atomic store operand must have integer, pointer, or floating point "
3736  "type!",
3737  ElTy, &SI);
3738  checkAtomicMemAccessSize(ElTy, &SI);
3739  } else {
3740  Assert(SI.getSyncScopeID() == SyncScope::System,
3741  "Non-atomic store cannot have SynchronizationScope specified", &SI);
3742  }
3743  visitInstruction(SI);
3744 }
3745 
3746 /// Check that SwiftErrorVal is used as a swifterror argument in CS.
3747 void Verifier::verifySwiftErrorCall(CallBase &Call,
3748  const Value *SwiftErrorVal) {
3749  for (const auto &I : llvm::enumerate(Call.args())) {
3750  if (I.value() == SwiftErrorVal) {
3751  Assert(Call.paramHasAttr(I.index(), Attribute::SwiftError),
3752  "swifterror value when used in a callsite should be marked "
3753  "with swifterror attribute",
3754  SwiftErrorVal, Call);
3755  }
3756  }
3757 }
3758 
3759 void Verifier::verifySwiftErrorValue(const Value *SwiftErrorVal) {
3760  // Check that swifterror value is only used by loads, stores, or as
3761  // a swifterror argument.
3762  for (const User *U : SwiftErrorVal->users()) {
3763  Assert(isa<LoadInst>(U) || isa<StoreInst>(U) || isa<CallInst>(U) ||
3764  isa<InvokeInst>(U),
3765  "swifterror value can only be loaded and stored from, or "
3766  "as a swifterror argument!",
3767  SwiftErrorVal, U);
3768  // If it is used by a store, check it is the second operand.
3769  if (auto StoreI = dyn_cast<StoreInst>(U))
3770  Assert(StoreI->getOperand(1) == SwiftErrorVal,
3771  "swifterror value should be the second operand when used "
3772  "by stores", SwiftErrorVal, U);
3773  if (auto *Call = dyn_cast<CallBase>(U))
3774  verifySwiftErrorCall(*const_cast<CallBase *>(Call), SwiftErrorVal);
3775  }
3776 }
3777 
3778 void Verifier::visitAllocaInst(AllocaInst &AI) {
3779  SmallPtrSet<Type*, 4> Visited;
3780  Assert(AI.getAllocatedType()->isSized(&Visited),
3781  "Cannot allocate unsized type", &AI);
3783  "Alloca array size must have integer type", &AI);
3785  "huge alignment values are unsupported", &AI);
3786 
3787  if (AI.isSwiftError()) {
3788  verifySwiftErrorValue(&AI);
3789  }
3790 
3791  visitInstruction(AI);
3792 }
3793 
3794 void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
3795  Type *ElTy = CXI.getOperand(1)->getType();
3796  Assert(ElTy->isIntOrPtrTy(),
3797  "cmpxchg operand must have integer or pointer type", ElTy, &CXI);
3798  checkAtomicMemAccessSize(ElTy, &CXI);
3799  visitInstruction(CXI);
3800 }
3801 
3802 void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
3804  "atomicrmw instructions cannot be unordered.", &RMWI);
3805  auto Op = RMWI.getOperation();
3806  Type *ElTy = RMWI.getOperand(1)->getType();
3807  if (Op == AtomicRMWInst::Xchg) {
3808  Assert(ElTy->isIntegerTy() || ElTy->isFloatingPointTy(), "atomicrmw " +
3810  " operand must have integer or floating point type!",
3811  &RMWI, ElTy);
3812  } else if (AtomicRMWInst::isFPOperation(Op)) {
3813  Assert(ElTy->isFloatingPointTy(), "atomicrmw " +
3815  " operand must have floating point type!",
3816  &RMWI, ElTy);
3817  } else {
3818  Assert(ElTy->isIntegerTy(), "atomicrmw " +
3820  " operand must have integer type!",
3821  &RMWI, ElTy);
3822  }
3823  checkAtomicMemAccessSize(ElTy, &RMWI);
3825  "Invalid binary operation!", &RMWI);
3826  visitInstruction(RMWI);
3827 }
3828 
3829 void Verifier::visitFenceInst(FenceInst &FI) {
3830  const AtomicOrdering Ordering = FI.getOrdering();
3831  Assert(Ordering == AtomicOrdering::Acquire ||
3832  Ordering == AtomicOrdering::Release ||
3833  Ordering == AtomicOrdering::AcquireRelease ||
3835  "fence instructions may only have acquire, release, acq_rel, or "
3836  "seq_cst ordering.",
3837  &FI);
3838  visitInstruction(FI);
3839 }
3840 
3841 void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
3843  EVI.getIndices()) == EVI.getType(),
3844  "Invalid ExtractValueInst operands!", &EVI);
3845 
3846  visitInstruction(EVI);
3847 }
3848 
3849 void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
3851  IVI.getIndices()) ==
3852  IVI.getOperand(1)->getType(),
3853  "Invalid InsertValueInst operands!", &IVI);
3854 
3855  visitInstruction(IVI);
3856 }
3857 
3858 static Value *getParentPad(Value *EHPad) {
3859  if (auto *FPI = dyn_cast<FuncletPadInst>(EHPad))
3860  return FPI->getParentPad();
3861 
3862  return cast<CatchSwitchInst>(EHPad)->getParentPad();
3863 }
3864 
3865 void Verifier::visitEHPadPredecessors(Instruction &I) {
3866  assert(I.isEHPad());
3867 
3868  BasicBlock *BB = I.getParent();
3869  Function *F = BB->getParent();
3870 
3871  Assert(BB != &F->getEntryBlock(), "EH pad cannot be in entry block.", &I);
3872 
3873  if (auto *LPI = dyn_cast<LandingPadInst>(&I)) {
3874  // The landingpad instruction defines its parent as a landing pad block. The
3875  // landing pad block may be branched to only by the unwind edge of an
3876  // invoke.
3877  for (BasicBlock *PredBB : predecessors(BB)) {
3878  const auto *II = dyn_cast<InvokeInst>(PredBB->getTerminator());
3879  Assert(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
3880  "Block containing LandingPadInst must be jumped to "
3881  "only by the unwind edge of an invoke.",
3882  LPI);
3883  }
3884  return;
3885  }
3886  if (auto *CPI = dyn_cast<CatchPadInst>(&I)) {
3887  if (!pred_empty(BB))
3888  Assert(BB->getUniquePredecessor() == CPI->getCatchSwitch()->getParent(),
3889  "Block containg CatchPadInst must be jumped to "
3890  "only by its catchswitch.",
3891  CPI);
3892  Assert(BB != CPI->getCatchSwitch()->getUnwindDest(),
3893  "Catchswitch cannot unwind to one of its catchpads",
3894  CPI->getCatchSwitch(), CPI);
3895  return;
3896  }
3897 
3898  // Verify that each pred has a legal terminator with a legal to/from EH
3899  // pad relationship.
3900  Instruction *ToPad = &I;
3901  Value *ToPadParent = getParentPad(ToPad);
3902  for (BasicBlock *PredBB : predecessors(BB)) {
3903  Instruction *TI = PredBB->getTerminator();
3904  Value *FromPad;
3905  if (auto *II = dyn_cast<InvokeInst>(TI)) {
3906  Assert(II->getUnwindDest() == BB && II->getNormalDest() != BB,
3907  "EH pad must be jumped to via an unwind edge", ToPad, II);
3908  if (auto Bundle = II->getOperandBundle(LLVMContext::OB_funclet))
3909  FromPad = Bundle->Inputs[0];
3910  else
3911  FromPad = ConstantTokenNone::get(II->getContext());
3912  } else if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) {
3913  FromPad = CRI->getOperand(0);
3914  Assert(FromPad != ToPadParent, "A cleanupret must exit its cleanup", CRI);
3915  } else if (auto *CSI = dyn_cast<CatchSwitchInst>(TI)) {
3916  FromPad = CSI;
3917  } else {
3918  Assert(false, "EH pad must be jumped to via an unwind edge", ToPad, TI);
3919  }
3920 
3921  // The edge may exit from zero or more nested pads.
3922  SmallSet<Value *, 8> Seen;
3923  for (;; FromPad = getParentPad(FromPad)) {
3924  Assert(FromPad != ToPad,
3925  "EH pad cannot handle exceptions raised within it", FromPad, TI);
3926  if (FromPad == ToPadParent) {
3927  // This is a legal unwind edge.
3928  break;
3929  }
3930  Assert(!isa<ConstantTokenNone>(FromPad),
3931  "A single unwind edge may only enter one EH pad", TI);
3932  Assert(Seen.insert(FromPad).second,
3933  "EH pad jumps through a cycle of pads", FromPad);
3934  }
3935  }
3936 }
3937 
3938 void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
3939  // The landingpad instruction is ill-formed if it doesn't have any clauses and
3940  // isn't a cleanup.
3941  Assert(LPI.getNumClauses() > 0 || LPI.isCleanup(),
3942  "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
3943 
3944  visitEHPadPredecessors(LPI);
3945 
3946  if (!LandingPadResultTy)
3947  LandingPadResultTy = LPI.getType();
3948  else
3949  Assert(LandingPadResultTy == LPI.getType(),
3950  "The landingpad instruction should have a consistent result type "
3951  "inside a function.",
3952  &LPI);
3953 
3954  Function *F = LPI.getParent()->getParent();
3955  Assert(F->hasPersonalityFn(),
3956  "LandingPadInst needs to be in a function with a personality.", &LPI);
3957 
3958  // The landingpad instruction must be the first non-PHI instruction in the
3959  // block.
3960  Assert(LPI.getParent()->getLandingPadInst() == &LPI,
3961  "LandingPadInst not the first non-PHI instruction in the block.",
3962  &LPI);
3963 
3964  for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
3965  Constant *Clause = LPI.getClause(i);
3966  if (LPI.isCatch(i)) {
3967  Assert(isa<PointerType>(Clause->getType()),
3968  "Catch operand does not have pointer type!", &LPI);
3969  } else {
3970  Assert(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
3971  Assert(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
3972  "Filter operand is not an array of constants!", &LPI);
3973  }
3974  }
3975 
3976  visitInstruction(LPI);
3977 }
3978 
3979 void Verifier::visitResumeInst(ResumeInst &RI) {
3981  "ResumeInst needs to be in a function with a personality.", &RI);
3982 
3983  if (!LandingPadResultTy)
3984  LandingPadResultTy = RI.getValue()->getType();
3985  else
3986  Assert(LandingPadResultTy == RI.getValue()->getType(),
3987  "The resume instruction should have a consistent result type "
3988  "inside a function.",
3989  &RI);
3990 
3991  visitTerminator(RI);
3992 }
3993 
3994 void Verifier::visitCatchPadInst(CatchPadInst &CPI) {
3995  BasicBlock *BB = CPI.getParent();
3996 
3997  Function *F = BB->getParent();
3998  Assert(F->hasPersonalityFn(),
3999  "CatchPadInst needs to be in a function with a personality.", &CPI);
4000 
4001  Assert(isa<CatchSwitchInst>(CPI.getParentPad()),
4002  "CatchPadInst needs to be directly nested in a CatchSwitchInst.",
4003  CPI.getParentPad());
4004 
4005  // The catchpad instruction must be the first non-PHI instruction in the
4006  // block.
4007  Assert(BB->getFirstNonPHI() == &CPI,
4008  "CatchPadInst not the first non-PHI instruction in the block.", &CPI);
4009 
4010  visitEHPadPredecessors(CPI);
4011  visitFuncletPadInst(CPI);
4012 }
4013 
4014 void Verifier::visitCatchReturnInst(CatchReturnInst &CatchReturn) {
4015  Assert(isa<CatchPadInst>(CatchReturn.getOperand(0)),
4016  "CatchReturnInst needs to be provided a CatchPad", &CatchReturn,
4017  CatchReturn.getOperand(0));
4018 
4019  visitTerminator(CatchReturn);
4020 }
4021 
4022 void Verifier::visitCleanupPadInst(CleanupPadInst &CPI) {
4023  BasicBlock *BB = CPI.getParent();
4024 
4025  Function *F = BB->getParent();
4026  Assert(F->hasPersonalityFn(),
4027  "CleanupPadInst needs to be in a function with a personality.", &CPI);
4028 
4029  // The cleanuppad instruction must be the first non-PHI instruction in the
4030  // block.
4031  Assert(BB->getFirstNonPHI() == &CPI,
4032  "CleanupPadInst not the first non-PHI instruction in the block.",
4033  &CPI);
4034 
4035  auto *ParentPad = CPI.getParentPad();
4036  Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
4037  "CleanupPadInst has an invalid parent.", &CPI);
4038 
4039  visitEHPadPredecessors(CPI);
4040  visitFuncletPadInst(CPI);
4041 }
4042 
4043 void Verifier::visitFuncletPadInst(FuncletPadInst &FPI) {
4044  User *FirstUser = nullptr;
4045  Value *FirstUnwindPad = nullptr;
4046  SmallVector<FuncletPadInst *, 8> Worklist({&FPI});
4048 
4049  while (!Worklist.empty()) {
4050  FuncletPadInst *CurrentPad = Worklist.pop_back_val();
4051  Assert(Seen.insert(CurrentPad).second,
4052  "FuncletPadInst must not be nested within itself", CurrentPad);
4053  Value *UnresolvedAncestorPad = nullptr;
4054  for (User *U : CurrentPad->users()) {
4055  BasicBlock *UnwindDest;
4056  if (auto *CRI = dyn_cast<CleanupReturnInst>(U)) {
4057  UnwindDest = CRI->getUnwindDest();
4058  } else if (auto *CSI = dyn_cast<CatchSwitchInst>(U)) {
4059  // We allow catchswitch unwind to caller to nest
4060  // within an outer pad that unwinds somewhere else,
4061  // because catchswitch doesn't have a nounwind variant.
4062  // See e.g. SimplifyCFGOpt::SimplifyUnreachable.
4063  if (CSI->unwindsToCaller())
4064  continue;
4065  UnwindDest = CSI->getUnwindDest();
4066  } else if (auto *II = dyn_cast<InvokeInst>(U)) {
4067  UnwindDest = II->getUnwindDest();
4068  } else if (isa<CallInst>(U)) {
4069  // Calls which don't unwind may be found inside funclet
4070  // pads that unwind somewhere else. We don't *require*
4071  // such calls to be annotated nounwind.
4072  continue;
4073  } else if (auto *CPI = dyn_cast<CleanupPadInst>(U)) {
4074  // The unwind dest for a cleanup can only be found by
4075  // recursive search. Add it to the worklist, and we'll
4076  // search for its first use that determines where it unwinds.
4077  Worklist.push_back(CPI);
4078  continue;
4079  } else {
4080  Assert(isa<CatchReturnInst>(U), "Bogus funclet pad use", U);
4081  continue;
4082  }
4083 
4084  Value *UnwindPad;
4085  bool ExitsFPI;
4086  if (UnwindDest) {
4087  UnwindPad = UnwindDest->getFirstNonPHI();
4088  if (!cast<Instruction>(UnwindPad)->isEHPad())
4089  continue;
4090  Value *UnwindParent = getParentPad(UnwindPad);
4091  // Ignore unwind edges that don't exit CurrentPad.
4092  if (UnwindParent == CurrentPad)
4093  continue;
4094  // Determine whether the original funclet pad is exited,
4095  // and if we are scanning nested pads determine how many
4096  // of them are exited so we can stop searching their
4097  // children.
4098  Value *ExitedPad = CurrentPad;
4099  ExitsFPI = false;
4100  do {
4101  if (ExitedPad == &FPI) {
4102  ExitsFPI = true;
4103  // Now we can resolve any ancestors of CurrentPad up to
4104  // FPI, but not including FPI since we need to make sure
4105  // to check all direct users of FPI for consistency.
4106  UnresolvedAncestorPad = &FPI;
4107  break;
4108  }
4109  Value *ExitedParent = getParentPad(ExitedPad);
4110  if (ExitedParent == UnwindParent) {
4111  // ExitedPad is the ancestor-most pad which this unwind
4112  // edge exits, so we can resolve up to it, meaning that
4113  // ExitedParent is the first ancestor still unresolved.
4114  UnresolvedAncestorPad = ExitedParent;
4115  break;
4116  }
4117  ExitedPad = ExitedParent;
4118  } while (!isa<ConstantTokenNone>(ExitedPad));
4119  } else {
4120  // Unwinding to caller exits all pads.
4121  UnwindPad = ConstantTokenNone::get(FPI.getContext());
4122  ExitsFPI = true;
4123  UnresolvedAncestorPad = &FPI;
4124  }
4125 
4126  if (ExitsFPI) {
4127  // This unwind edge exits FPI. Make sure it agrees with other
4128  // such edges.
4129  if (FirstUser) {
4130  Assert(UnwindPad == FirstUnwindPad, "Unwind edges out of a funclet "
4131  "pad must have the same unwind "
4132  "dest",
4133  &FPI, U, FirstUser);
4134  } else {
4135  FirstUser = U;
4136  FirstUnwindPad = UnwindPad;
4137  // Record cleanup sibling unwinds for verifySiblingFuncletUnwinds
4138  if (isa<CleanupPadInst>(&FPI) && !isa<ConstantTokenNone>(UnwindPad) &&
4139  getParentPad(UnwindPad) == getParentPad(&FPI))
4140  SiblingFuncletInfo[&FPI] = cast<Instruction>(U);
4141  }
4142  }
4143  // Make sure we visit all uses of FPI, but for nested pads stop as
4144  // soon as we know where they unwind to.
4145  if (CurrentPad != &FPI)
4146  break;
4147  }
4148  if (UnresolvedAncestorPad) {
4149  if (CurrentPad == UnresolvedAncestorPad) {
4150  // When CurrentPad is FPI itself, we don't mark it as resolved even if
4151  // we've found an unwind edge that exits it, because we need to verify
4152  // all direct uses of FPI.
4153  assert(CurrentPad == &FPI);
4154  continue;
4155  }
4156  // Pop off the worklist any nested pads that we've found an unwind
4157  // destination for. The pads on the worklist are the uncles,
4158  // great-uncles, etc. of CurrentPad. We've found an unwind destination
4159  // for all ancestors of CurrentPad up to but not including
4160  // UnresolvedAncestorPad.
4161  Value *ResolvedPad = CurrentPad;
4162  while (!Worklist.empty()) {
4163  Value *UnclePad = Worklist.back();
4164  Value *AncestorPad = getParentPad(UnclePad);
4165  // Walk ResolvedPad up the ancestor list until we either find the
4166  // uncle's parent or the last resolved ancestor.
4167  while (ResolvedPad != AncestorPad) {
4168  Value *ResolvedParent = getParentPad(ResolvedPad);
4169  if (ResolvedParent == UnresolvedAncestorPad) {
4170  break;
4171  }
4172  ResolvedPad = ResolvedParent;
4173  }
4174  // If the resolved ancestor search didn't find the uncle's parent,
4175  // then the uncle is not yet resolved.
4176  if (ResolvedPad != AncestorPad)
4177  break;
4178  // This uncle is resolved, so pop it from the worklist.
4179  Worklist.pop_back();
4180  }
4181  }
4182  }
4183 
4184  if (FirstUnwindPad) {
4185  if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FPI.getParentPad())) {
4186  BasicBlock *SwitchUnwindDest = CatchSwitch->getUnwindDest();
4187  Value *SwitchUnwindPad;
4188  if (SwitchUnwindDest)
4189  SwitchUnwindPad = SwitchUnwindDest->getFirstNonPHI();
4190  else
4191  SwitchUnwindPad = ConstantTokenNone::get(FPI.getContext());
4192  Assert(SwitchUnwindPad == FirstUnwindPad,
4193  "Unwind edges out of a catch must have the same unwind dest as "
4194  "the parent catchswitch",
4195  &FPI, FirstUser, CatchSwitch);
4196  }
4197  }
4198 
4199  visitInstruction(FPI);
4200 }
4201 
4202 void Verifier::visitCatchSwitchInst(CatchSwitchInst &CatchSwitch) {
4203  BasicBlock *BB = CatchSwitch.getParent();
4204 
4205  Function *F = BB->getParent();
4206  Assert(F->hasPersonalityFn(),
4207  "CatchSwitchInst needs to be in a function with a personality.",
4208  &CatchSwitch);
4209 
4210  // The catchswitch instruction must be the first non-PHI instruction in the
4211  // block.
4212  Assert(BB->getFirstNonPHI() == &CatchSwitch,
4213  "CatchSwitchInst not the first non-PHI instruction in the block.",
4214  &CatchSwitch);
4215 
4216  auto *ParentPad = CatchSwitch.getParentPad();
4217  Assert(isa<ConstantTokenNone>(ParentPad) || isa<FuncletPadInst>(ParentPad),
4218  "CatchSwitchInst has an invalid parent.", ParentPad);
4219 
4220  if (BasicBlock *UnwindDest = CatchSwitch.getUnwindDest()) {
4221  Instruction *I = UnwindDest->getFirstNonPHI();
4222  Assert(I->isEHPad() && !isa<LandingPadInst>(I),
4223  "CatchSwitchInst must unwind to an EH block which is not a "
4224  "landingpad.",
4225  &CatchSwitch);
4226 
4227  // Record catchswitch sibling unwinds for verifySiblingFuncletUnwinds
4228  if (getParentPad(I) == ParentPad)
4229  SiblingFuncletInfo[&CatchSwitch] = &CatchSwitch;
4230  }
4231 
4232  Assert(CatchSwitch.getNumHandlers() != 0,
4233  "CatchSwitchInst cannot have empty handler list", &CatchSwitch);
4234 
4235  for (BasicBlock *Handler : CatchSwitch.handlers()) {
4236  Assert(isa<CatchPadInst>(Handler->getFirstNonPHI()),
4237  "CatchSwitchInst handlers must be catchpads", &CatchSwitch, Handler);
4238  }
4239 
4240  visitEHPadPredecessors(CatchSwitch);
4241  visitTerminator(CatchSwitch);
4242 }
4243 
4244 void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
4245  Assert(isa<CleanupPadInst>(CRI.getOperand(0)),
4246  "CleanupReturnInst needs to be provided a CleanupPad", &CRI,
4247  CRI.getOperand(0));
4248 
4249  if (BasicBlock *UnwindDest = CRI.getUnwindDest()) {
4250  Instruction *I = UnwindDest->getFirstNonPHI();
4251  Assert(I->isEHPad() && !isa<LandingPadInst>(I),
4252  "CleanupReturnInst must unwind to an EH block which is not a "
4253  "landingpad.",
4254  &CRI);
4255  }
4256 
4257  visitTerminator(CRI);
4258 }
4259 
4260 void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
4261  Instruction *Op = cast<Instruction>(I.getOperand(i));
4262  // If the we have an invalid invoke, don't try to compute the dominance.
4263  // We already reject it in the invoke specific checks and the dominance
4264  // computation doesn't handle multiple edges.
4265  if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
4266  if (II->getNormalDest() == II->getUnwindDest())
4267  return;
4268  }
4269 
4270  // Quick check whether the def has already been encountered in the same block.
4271  // PHI nodes are not checked to prevent accepting preceding PHIs, because PHI
4272  // uses are defined to happen on the incoming edge, not at the instruction.
4273  //
4274  // FIXME: If this operand is a MetadataAsValue (wrapping a LocalAsMetadata)
4275  // wrapping an SSA value, assert that we've already encountered it. See
4276  // related FIXME in Mapper::mapLocalAsMetadata in ValueMapper.cpp.
4277  if (!isa<PHINode>(I) && InstsInThisBlock.count(Op))
4278  return;
4279 
4280  const Use &U = I.getOperandUse(i);
4281  Assert(DT.dominates(Op, U),
4282  "Instruction does not dominate all uses!", Op, &I);
4283 }
4284 
4285 void Verifier::visitDereferenceableMetadata(Instruction& I, MDNode* MD) {
4286  Assert(I.getType()->isPointerTy(), "dereferenceable, dereferenceable_or_null "
4287  "apply only to pointer types", &I);
4288  Assert((isa<LoadInst>(I) || isa<IntToPtrInst>(I)),
4289  "dereferenceable, dereferenceable_or_null apply only to load"
4290  " and inttoptr instructions, use attributes for calls or invokes", &I);
4291  Assert(MD->getNumOperands() == 1, "dereferenceable, dereferenceable_or_null "
4292  "take one operand!", &I);
4293  ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0));
4294  Assert(CI && CI->getType()->isIntegerTy(64), "dereferenceable, "
4295  "dereferenceable_or_null metadata value must be an i64!", &I);
4296 }
4297 
4298 void Verifier::visitProfMetadata(Instruction &I, MDNode *MD) {
4299  Assert(MD->getNumOperands() >= 2,
4300  "!prof annotations should have no less than 2 operands", MD);
4301 
4302  // Check first operand.
4303  Assert(MD->getOperand(0) != nullptr, "first operand should not be null", MD);
4304  Assert(isa<MDString>(MD->getOperand(0)),
4305  "expected string with name of the !prof annotation", MD);
4306  MDString *MDS = cast<MDString>(MD->getOperand(0));
4307  StringRef ProfName = MDS->getString();
4308 
4309  // Check consistency of !prof branch_weights metadata.
4310  if (ProfName.equals("branch_weights")) {
4311  if (isa<InvokeInst>(&I)) {
4312  Assert(MD->getNumOperands() == 2 || MD->getNumOperands() == 3,
4313  "Wrong number of InvokeInst branch_weights operands", MD);
4314  } else {
4315  unsigned ExpectedNumOperands = 0;
4316  if (BranchInst *BI = dyn_cast<BranchInst>(&I))
4317  ExpectedNumOperands = BI->getNumSuccessors();
4318  else if (SwitchInst *SI = dyn_cast<SwitchInst>(&I))
4319  ExpectedNumOperands = SI->getNumSuccessors();
4320  else if (isa<CallInst>(&I))
4321  ExpectedNumOperands = 1;
4322  else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(&I))
4323  ExpectedNumOperands = IBI->getNumDestinations();
4324  else if (isa<SelectInst>(&I))
4325  ExpectedNumOperands = 2;
4326  else
4327  CheckFailed("!prof branch_weights are not allowed for this instruction",
4328  MD);
4329 
4330  Assert(MD->getNumOperands() == 1 + ExpectedNumOperands,
4331  "Wrong number of operands", MD);
4332  }
4333  for (unsigned i = 1; i < MD->getNumOperands(); ++i) {
4334  auto &MDO = MD->getOperand(i);
4335  Assert(MDO, "second operand should not be null", MD);
4336  Assert(mdconst::dyn_extract<ConstantInt>(MDO),
4337  "!prof brunch_weights operand is not a const int");
4338  }
4339  }
4340 }
4341 
4342 void Verifier::visitAnnotationMetadata(MDNode *Annotation) {
4343  Assert(isa<MDTuple>(Annotation), "annotation must be a tuple");
4344  Assert(Annotation->getNumOperands() >= 1,
4345  "annotation must have at least one operand");
4346  for (const MDOperand &Op : Annotation->operands())
4347  Assert(isa<MDString>(Op.get()), "operands must be strings");
4348 }
4349 
4350 /// verifyInstruction - Verify that an instruction is well formed.
4351 ///
4352 void Verifier::visitInstruction(Instruction &I) {
4353  BasicBlock *BB = I.getParent();
4354  Assert(BB, "Instruction not embedded in basic block!", &I);
4355 
4356  if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
4357  for (User *U : I.users()) {
4358  Assert(U != (User *)&I || !DT.isReachableFromEntry(BB),
4359  "Only PHI nodes may reference their own value!", &I);
4360  }
4361  }
4362 
4363  // Check that void typed values don't have names
4364  Assert(!I.getType()->isVoidTy() || !I.hasName(),
4365  "Instruction has a name, but provides a void value!", &I);
4366 
4367  // Check that the return value of the instruction is either void or a legal
4368  // value type.
4369  Assert(I.getType()->isVoidTy() || I.getType()->isFirstClassType(),
4370  "Instruction returns a non-scalar type!", &I);
4371 
4372  // Check that the instruction doesn't produce metadata. Calls are already
4373  // checked against the callee type.
4374  Assert(!I.getType()->isMetadataTy() || isa<CallInst>(I) || isa<InvokeInst>(I),
4375  "Invalid use of metadata!", &I);
4376 
4377  // Check that all uses of the instruction, if they are instructions
4378  // themselves, actually have parent basic blocks. If the use is not an
4379  // instruction, it is an error!
4380  for (Use &U : I.uses()) {
4381  if (Instruction *Used = dyn_cast<Instruction>(U.getUser()))
4382  Assert(Used->getParent() != nullptr,
4383  "Instruction referencing"
4384  " instruction not embedded in a basic block!",
4385  &I, Used);
4386  else {
4387  CheckFailed("Use of instruction is not an instruction!", U);
4388  return;
4389  }
4390  }
4391 
4392  // Get a pointer to the call base of the instruction if it is some form of
4393  // call.
4394  const CallBase *CBI = dyn_cast<CallBase>(&I);
4395 
4396  for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
4397  Assert(I.getOperand(i) != nullptr, "Instruction has null operand!", &I);
4398 
4399  // Check to make sure that only first-class-values are operands to
4400  // instructions.
4401  if (!I.getOperand(i)->getType()->isFirstClassType()) {
4402  Assert(false, "Instruction operands must be first-class values!", &I);
4403  }
4404 
4405  if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
4406  // Check to make sure that the "address of" an intrinsic function is never
4407  // taken.
4408  Assert(!F->isIntrinsic() ||
4409  (CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i)),
4410  "Cannot take the address of an intrinsic!", &I);
4411  Assert(
4412  !F->isIntrinsic() || isa<CallInst>(I) ||
4413  F->getIntrinsicID() == Intrinsic::donothing ||
4414  F->getIntrinsicID() == Intrinsic::seh_try_begin ||
4415  F->getIntrinsicID() == Intrinsic::seh_try_end ||
4416  F->getIntrinsicID() == Intrinsic::seh_scope_begin ||
4417  F->getIntrinsicID() == Intrinsic::seh_scope_end ||
4418  F->getIntrinsicID() == Intrinsic::coro_resume ||
4419  F->getIntrinsicID() == Intrinsic::coro_destroy ||
4420  F->getIntrinsicID() == Intrinsic::experimental_patchpoint_void ||
4421  F->getIntrinsicID() == Intrinsic::experimental_patchpoint_i64 ||
4422  F->getIntrinsicID() == Intrinsic::experimental_gc_statepoint ||
4423  F->getIntrinsicID() == Intrinsic::wasm_rethrow,
4424  "Cannot invoke an intrinsic other than donothing, patchpoint, "
4425  "statepoint, coro_resume or coro_destroy",
4426  &I);
4427  Assert(F->getParent() == &M, "Referencing function in another module!",
4428  &I, &M, F, F->getParent());
4429  } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
4430  Assert(OpBB->getParent() == BB->getParent(),
4431  "Referring to a basic block in another function!", &I);
4432  } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
4433  Assert(OpArg->getParent() == BB->getParent(),
4434  "Referring to an argument in another function!", &I);
4435  } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
4436  Assert(GV->getParent() == &M, "Referencing global in another module!", &I,
4437  &M, GV, GV->getParent());
4438  } else if (isa<Instruction>(I.getOperand(i))) {
4439  verifyDominatesUse(I, i);
4440  } else if (isa<InlineAsm>(I.getOperand(i))) {
4441  Assert(CBI && &CBI->getCalledOperandUse() == &I.getOperandUse(i),
4442  "Cannot take the address of an inline asm!", &I);
4443  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
4444  if (CE->getType()->isPtrOrPtrVectorTy()) {
4445  // If we have a ConstantExpr pointer, we need to see if it came from an
4446  // illegal bitcast.
4447  visitConstantExprsRecursively(CE);
4448  }
4449  }
4450  }
4451 
4452  if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
4453  Assert(I.getType()->isFPOrFPVectorTy(),
4454  "fpmath requires a floating point result!", &I);
4455  Assert(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
4456  if (ConstantFP *CFP0 =
4457  mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
4458  const APFloat &Accuracy = CFP0->getValueAPF();
4459  Assert(&Accuracy.getSemantics() == &APFloat::IEEEsingle(),
4460  "fpmath accuracy must have float type", &I);
4461  Assert(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
4462  "fpmath accuracy not a positive number!", &I);
4463  } else {
4464  Assert(false, "invalid fpmath accuracy!", &I);
4465  }
4466  }
4467 
4468  if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
4469  Assert(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
4470  "Ranges are only for loads, calls and invokes!", &I);
4471  visitRangeMetadata(I, Range, I.getType());
4472  }
4473 
4474  if (I.getMetadata(LLVMContext::MD_nonnull)) {
4475  Assert(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
4476  &I);
4477  Assert(isa<LoadInst>(I),
4478  "nonnull applies only to load instructions, use attributes"
4479  " for calls or invokes",
4480  &I);
4481  }
4482 
4483  if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable))
4484  visitDereferenceableMetadata(I, MD);
4485 
4486  if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
4487  visitDereferenceableMetadata(I, MD);
4488 
4489  if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa))
4490  TBAAVerifyHelper.visitTBAAMetadata(I, TBAA);
4491 
4492  if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
4493  Assert(I.getType()->isPointerTy(), "align applies only to pointer types",
4494  &I);
4495  Assert(isa<LoadInst>(I), "align applies only to load instructions, "
4496  "use attributes for calls or invokes", &I);
4497  Assert(AlignMD->getNumOperands() == 1, "align takes one operand!", &I);
4498  ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(AlignMD->getOperand(0));
4499  Assert(CI && CI->getType()->isIntegerTy(64),
4500  "align metadata value must be an i64!", &I);
4501  uint64_t Align = CI->getZExtValue();
4503  "align metadata value must be a power of 2!", &I);
4505  "alignment is larger that implementation defined limit", &I);
4506  }
4507 
4508  if (MDNode *MD = I.getMetadata(LLVMContext::MD_prof))
4509  visitProfMetadata(I, MD);
4510 
4511  if (MDNode *Annotation = I.getMetadata(LLVMContext::MD_annotation))
4512  visitAnnotationMetadata(Annotation);
4513 
4514  if (MDNode *N = I.getDebugLoc().getAsMDNode()) {
4515  AssertDI(isa<DILocation>(N), "invalid !dbg metadata attachment", &I, N);
4516  visitMDNode(*N, AreDebugLocsAllowed::Yes);
4517  }
4518 
4519  if (auto *DII = dyn_cast<DbgVariableIntrinsic>(&I)) {
4520  verifyFragmentExpression(*DII);
4521  verifyNotEntryValue(*DII);
4522  }
4523 
4525  I.getAllMetadata(MDs);
4526  for (auto Attachment : MDs) {
4527  unsigned Kind = Attachment.first;
4528  auto AllowLocs =
4529  (Kind == LLVMContext::MD_dbg || Kind == LLVMContext::MD_loop)
4530  ? AreDebugLocsAllowed::Yes
4531  : AreDebugLocsAllowed::No;
4532  visitMDNode(*Attachment.second, AllowLocs);
4533  }
4534 
4535  InstsInThisBlock.insert(&I);
4536 }
4537 
4538 /// Allow intrinsics to be verified in different ways.
4539 void Verifier::visitIntrinsicCall(Intrinsic::ID ID, CallBase &Call) {
4540  Function *IF = Call.getCalledFunction();
4541  Assert(IF->isDeclaration(), "Intrinsic functions should never be defined!",
4542  IF);
4543 
4544  // Verify that the intrinsic prototype lines up with what the .td files
4545  // describe.
4546  FunctionType *IFTy = IF->getFunctionType();
4547  bool IsVarArg = IFTy->isVarArg();
4548 
4552 
4553  // Walk the descriptors to extract overloaded types.
4554  SmallVector<Type *, 4> ArgTys;
4558  "Intrinsic has incorrect return type!", IF);
4560  "Intrinsic has incorrect argument type!", IF);
4561 
4562  // Verify if the intrinsic call matches the vararg property.
4563  if (IsVarArg)
4565  "Intrinsic was not defined with variable arguments!", IF);
4566  else
4568  "Callsite was not defined with variable arguments!", IF);
4569 
4570  // All descriptors should be absorbed by now.
4571  Assert(TableRef.empty(), "Intrinsic has too few arguments!", IF);
4572 
4573  // Now that we have the intrinsic ID and the actual argument types (and we
4574  // know they are legal for the intrinsic!) get the intrinsic name through the
4575  // usual means. This allows us to verify the mangling of argument types into
4576  // the name.
4577  const std::string ExpectedName =
4578  Intrinsic::getName(ID, ArgTys, IF->getParent(), IFTy);
4579  Assert(ExpectedName == IF->getName(),
4580  "Intrinsic name not mangled correctly for type arguments! "
4581  "Should be: " +
4582  ExpectedName,
4583  IF);
4584 
4585  // If the intrinsic takes MDNode arguments, verify that they are either global
4586  // or are local to *this* function.
4587  for (Value *V : Call.args()) {
4588  if (auto *MD = dyn_cast<MetadataAsValue>(V))
4589  visitMetadataAsValue(*MD, Call.getCaller());
4590  if (auto *Const = dyn_cast<Constant>(V))
4591  Assert(!Const->getType()->isX86_AMXTy(),
4592  "const x86_amx is not allowed in argument!");
4593  }
4594 
4595  switch (ID) {
4596  default:
4597  break;
4598  case Intrinsic::assume: {
4599  for (auto &Elem : Call.bundle_op_infos()) {
4600  Assert(Elem.Tag->getKey() == "ignore" ||
4601  Attribute::isExistingAttribute(Elem.Tag->getKey()),
4602  "tags must be valid attribute names");
4604  Attribute::getAttrKindFromName(Elem.Tag->getKey());
4605  unsigned ArgCount = Elem.End - Elem.Begin;
4606  if (Kind == Attribute::Alignment) {
4607  Assert(ArgCount <= 3 && ArgCount >= 2,
4608  "alignment assumptions should have 2 or 3 arguments");
4609  Assert(Call.getOperand(Elem.Begin)->getType()->isPointerTy(),
4610  "first argument should be a pointer");
4611  Assert(Call.getOperand(Elem.Begin + 1)->getType()->isIntegerTy(),
4612  "second argument should be an integer");
4613  if (ArgCount == 3)
4614  Assert(Call.getOperand(Elem.Begin + 2)->getType()->isIntegerTy(),
4615  "third argument should be an integer if present");
4616  return;
4617  }
4618  Assert(ArgCount <= 2, "to many arguments");
4619  if (Kind == Attribute::None)
4620  break;
4622  Assert(ArgCount == 2, "this attribute should have 2 arguments");
4623  Assert(isa<ConstantInt>(Call.getOperand(Elem.Begin + 1)),
4624  "the second argument should be a constant integral value");
4625  } else if (Attribute::canUseAsParamAttr(Kind)) {
4626  Assert((ArgCount) == 1, "this attribute should have one argument");
4627  } else if (Attribute::canUseAsFnAttr(Kind)) {
4628  Assert((ArgCount) == 0, "this attribute has no argument");
4629  }
4630  }
4631  break;
4632  }
4633  case Intrinsic::coro_id: {
4634  auto *InfoArg = Call.getArgOperand(3)->stripPointerCasts();
4635  if (isa<ConstantPointerNull>(InfoArg))
4636  break;
4637  auto *GV = dyn_cast<GlobalVariable>(InfoArg);
4638  Assert(GV && GV->isConstant() && GV->hasDefinitiveInitializer(),
4639  "info argument of llvm.coro.id must refer to an initialized "
4640  "constant");
4641  Constant *Init = GV->getInitializer();
4642  Assert(isa<ConstantStruct>(Init) || isa<ConstantArray>(Init),
4643  "info argument of llvm.coro.id must refer to either a struct or "
4644  "an array");
4645  break;
4646  }
4647 #define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC) \
4648  case Intrinsic::INTRINSIC:
4649 #include "llvm/IR/ConstrainedOps.def"
4650  visitConstrainedFPIntrinsic(cast<ConstrainedFPIntrinsic>(Call));
4651  break;
4652  case Intrinsic::dbg_declare: // llvm.dbg.declare
4653  Assert(isa<MetadataAsValue>(Call.getArgOperand(0)),
4654  "invalid llvm.dbg.declare intrinsic call 1", Call);
4655  visitDbgIntrinsic("declare", cast<DbgVariableIntrinsic>(Call));
4656  break;
4657  case Intrinsic::dbg_addr: // llvm.dbg.addr
4658  visitDbgIntrinsic("addr", cast<DbgVariableIntrinsic>(Call));
4659  break;
4660  case Intrinsic::dbg_value: // llvm.dbg.value
4661  visitDbgIntrinsic("value", cast<DbgVariableIntrinsic>(Call));
4662  break;
4663  case Intrinsic::dbg_label: // llvm.dbg.label
4664  visitDbgLabelIntrinsic("label", cast<DbgLabelInst>(Call));
4665  break;
4666  case Intrinsic::memcpy:
4667  case Intrinsic::memcpy_inline:
4668  case Intrinsic::memmove:
4669  case Intrinsic::memset: {
4670  const auto *MI = cast<MemIntrinsic>(&Call);
4671  auto IsValidAlignment = [&](unsigned Alignment) -> bool {
4672  return Alignment == 0 || isPowerOf2_32(Alignment);
4673  };
4674  Assert(IsValidAlignment(MI->getDestAlignment()),
4675  "alignment of arg 0 of memory intrinsic must be 0 or a power of 2",
4676  Call);
4677  if (const auto *MTI = dyn_cast<MemTransferInst>(MI)) {
4678  Assert(IsValidAlignment(MTI->getSourceAlignment()),
4679  "alignment of arg 1 of memory intrinsic must be 0 or a power of 2",
4680  Call);
4681  }
4682 
4683  break;
4684  }
4685  case Intrinsic::memcpy_element_unordered_atomic:
4686  case Intrinsic::memmove_element_unordered_atomic:
4687  case Intrinsic::memset_element_unordered_atomic: {
4688  const auto *AMI = cast<AtomicMemIntrinsic>(&Call);
4689 
4690  ConstantInt *ElementSizeCI =
4691  cast<ConstantInt>(AMI->getRawElementSizeInBytes());
4692  const APInt &ElementSizeVal = ElementSizeCI->getValue();
4693  Assert(ElementSizeVal.isPowerOf2(),
4694  "element size of the element-wise atomic memory intrinsic "
4695  "must be a power of 2",
4696  Call);
4697 
4698  auto IsValidAlignment = [&](uint64_t Alignment) {
4699  return isPowerOf2_64(Alignment) && ElementSizeVal.ule(Alignment);
4700  };
4701  uint64_t DstAlignment = AMI->getDestAlignment();
4702  Assert(IsValidAlignment(DstAlignment),
4703  "incorrect alignment of the destination argument", Call);
4704  if (const auto *AMT = dyn_cast<AtomicMemTransferInst>(AMI)) {
4705  uint64_t SrcAlignment = AMT->getSourceAlignment();
4706  Assert(IsValidAlignment(SrcAlignment),
4707  "incorrect alignment of the source argument", Call);
4708  }
4709  break;
4710  }
4711  case Intrinsic::call_preallocated_setup: {
4712  auto *NumArgs = dyn_cast<ConstantInt>(Call.getArgOperand(0));
4713  Assert(NumArgs != nullptr,
4714  "llvm.call.preallocated.setup argument must be a constant");
4715  bool FoundCall = false;
4716  for (User *U : Call.users()) {
4717  auto *UseCall = dyn_cast<CallBase>(U);
4718  Assert(UseCall != nullptr,
4719  "Uses of llvm.call.preallocated.setup must be calls");
4720  const Function *Fn = UseCall->getCalledFunction();
4721  if (Fn && Fn->getIntrinsicID() == Intrinsic::call_preallocated_arg) {
4722  auto *AllocArgIndex = dyn_cast<ConstantInt>(UseCall->getArgOperand(1));
4723  Assert(AllocArgIndex != nullptr,
4724  "llvm.call.preallocated.alloc arg index must be a constant");
4725  auto AllocArgIndexInt = AllocArgIndex->getValue();
4726  Assert(AllocArgIndexInt.sge(0) &&
4727  AllocArgIndexInt.slt(NumArgs->getValue()),
4728  "llvm.call.preallocated.alloc arg index must be between 0 and "
4729  "corresponding "
4730  "llvm.call.preallocated.setup's argument count");
4731  } else if (Fn && Fn->getIntrinsicID() ==
4732  Intrinsic::call_preallocated_teardown) {
4733  // nothing to do
4734  } else {
4735  Assert(!FoundCall, "Can have at most one call corresponding to a "
4736  "llvm.call.preallocated.setup");
4737  FoundCall = true;
4738  size_t NumPreallocatedArgs = 0;
4739  for (unsigned i = 0; i < UseCall->getNumArgOperands(); i++) {
4740  if (UseCall->paramHasAttr(i, Attribute::Preallocated)) {
4741  ++NumPreallocatedArgs;
4742  }
4743  }
4744  Assert(NumPreallocatedArgs != 0,
4745  "cannot use preallocated intrinsics on a call without "
4746  "preallocated arguments");
4747  Assert(NumArgs->equalsInt(NumPreallocatedArgs),
4748  "llvm.call.preallocated.setup arg size must be equal to number "
4749  "of preallocated arguments "
4750  "at call site",
4751  Call, *UseCall);
4752  // getOperandBundle() cannot be called if more than one of the operand
4753  // bundle exists. There is already a check elsewhere for this, so skip
4754  // here if we see more than one.
4755  if (UseCall->countOperandBundlesOfType(LLVMContext::OB_preallocated) >
4756  1) {
4757  return;
4758  }
4759  auto PreallocatedBundle =
4760  UseCall->getOperandBundle(LLVMContext::OB_preallocated);
4761  Assert(PreallocatedBundle,
4762  "Use of llvm.call.preallocated.setup outside intrinsics "
4763  "must be in \"preallocated\" operand bundle");
4764  Assert(PreallocatedBundle->Inputs.front().get() == &Call,
4765  "preallocated bundle must have token from corresponding "
4766  "llvm.call.preallocated.setup");
4767  }
4768  }
4769  break;
4770  }
4771  case Intrinsic::call_preallocated_arg: {
4772  auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
4773  Assert(Token && Token->getCalledFunction()->getIntrinsicID() ==
4774  Intrinsic::call_preallocated_setup,
4775  "llvm.call.preallocated.arg token argument must be a "
4776  "llvm.call.preallocated.setup");
4777  Assert(Call.hasFnAttr(Attribute::Preallocated),
4778  "llvm.call.preallocated.arg must be called with a \"preallocated\" "
4779  "call site attribute");
4780  break;
4781  }
4782  case Intrinsic::call_preallocated_teardown: {
4783  auto *Token = dyn_cast<CallBase>(Call.getArgOperand(0));
4784  Assert(Token && Token->getCalledFunction()->getIntrinsicID() ==
4785  Intrinsic::call_preallocated_setup,
4786  "llvm.call.preallocated.teardown token argument must be a "
4787  "llvm.call.preallocated.setup");
4788  break;
4789  }
4790  case Intrinsic::gcroot:
4791  case Intrinsic::gcwrite:
4792  case Intrinsic::gcread:
4793  if (ID == Intrinsic::gcroot) {
4794  AllocaInst *AI =
4795  dyn_cast<AllocaInst>(Call.getArgOperand(0)->stripPointerCasts());
4796  Assert(AI, "llvm.gcroot parameter #1 must be an alloca.", Call);
4797  Assert(isa<Constant>(Call.getArgOperand(1)),
4798  "llvm.gcroot parameter #2 must be a constant.", Call);
4799  if (!AI->getAllocatedType()->isPointerTy()) {
4800  Assert(!isa<ConstantPointerNull>(Call.getArgOperand(1)),
4801  "llvm.gcroot parameter #1 must either be a pointer alloca, "
4802  "or argument #2 must be a non-null constant.",
4803  Call);
4804  }
4805  }
4806 
4807  Assert(Call.getParent()->getParent()->hasGC(),
4808  "Enclosing function does not use GC.", Call);
4809  break;
4810  case Intrinsic::init_trampoline:
4811  Assert(isa<Function>(Call.getArgOperand(1)->stripPointerCasts()),
4812  "llvm.init_trampoline parameter #2 must resolve to a function.",
4813  Call);
4814  break;
4815  case Intrinsic::prefetch:
4816  Assert(cast<ConstantInt>(Call.getArgOperand(1))->getZExtValue() < 2 &&
4817  cast<ConstantInt>(Call.getArgOperand(2))->getZExtValue() < 4,
4818  "invalid arguments to llvm.prefetch", Call);
4819  break;
4820  case Intrinsic::stackprotector:
4821  Assert(isa<AllocaInst>(Call.getArgOperand(1)->stripPointerCasts()),
4822  "llvm.stackprotector parameter #2 must resolve to an alloca.", Call);
4823  break;
4824  case Intrinsic::localescape: {
4825  BasicBlock *BB = Call.getParent();
4826  Assert(BB == &BB->getParent()->front(),
4827  "llvm.localescape used outside of entry block", Call);
4828  Assert(!SawFrameEscape,
4829  "multiple calls to llvm.localescape in one function", Call);
4830  for (Value *Arg : Call.args()) {
4831  if (isa<ConstantPointerNull>(Arg))
4832  continue; // Null values are allowed as placeholders.
4833  auto *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
4834  Assert(AI && AI->isStaticAlloca(),
4835  "llvm.localescape only accepts static allocas", Call);
4836  }
4837  FrameEscapeInfo[BB->getParent()].first = Call.getNumArgOperands();
4838  SawFrameEscape = true;
4839  break;
4840  }
4841  case Intrinsic::localrecover: {
4842  Value *FnArg = Call.getArgOperand(0)-&g