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