1 //===- Verifier.h - LLVM IR Verifier ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the function verifier interface, that can be used for some
11 // sanity checking of input to the system, and for checking that transformations
12 // haven't done something bad.
14 // Note that this does not provide full 'java style' security and verifications,
15 // instead it just tries to ensure that code is well formed.
17 // To see what specifically is checked, look at the top of Verifier.cpp
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_IR_VERIFIER_H
22 #define LLVM_IR_VERIFIER_H
24 #include "llvm/IR/PassManager.h"
33 struct VerifierSupport;
35 /// Verify that the TBAA Metadatas are valid.
37 VerifierSupport *Diagnostic = nullptr;
39 /// Helper to diagnose a failure
40 template <typename... Tys> void CheckFailed(Tys &&... Args);
42 /// Cache of TBAA base nodes that have already been visited. This cachce maps
43 /// a node that has been visited to a pair (IsInvalid, BitWidth) where
45 /// \c IsInvalid is true iff the node is invalid.
46 /// \c BitWidth, if non-zero, is the bitwidth of the integer used to denoting
47 /// the offset of the access. If zero, only a zero offset is allowed.
49 /// \c BitWidth has no meaning if \c IsInvalid is true.
50 typedef std::pair<bool, unsigned> TBAABaseNodeSummary;
51 DenseMap<const MDNode *, TBAABaseNodeSummary> TBAABaseNodes;
53 /// Maps an alleged scalar TBAA node to a boolean that is true if the said
54 /// TBAA node is a valid scalar TBAA node or false otherwise.
55 DenseMap<const MDNode *, bool> TBAAScalarNodes;
57 /// \name Helper functions used by \c visitTBAAMetadata.
59 MDNode *getFieldNodeFromTBAABaseNode(Instruction &I, const MDNode *BaseNode,
61 TBAAVerifier::TBAABaseNodeSummary verifyTBAABaseNode(Instruction &I,
62 const MDNode *BaseNode);
63 TBAABaseNodeSummary verifyTBAABaseNodeImpl(Instruction &I,
64 const MDNode *BaseNode);
66 bool isValidScalarTBAANode(const MDNode *MD);
70 TBAAVerifier(VerifierSupport *Diagnostic = nullptr)
71 : Diagnostic(Diagnostic) {}
72 /// Visit an instruction and return true if it is valid, return false if an
73 /// invalid TBAA is attached.
74 bool visitTBAAMetadata(Instruction &I, const MDNode *MD);
77 /// \brief Check a function for errors, useful for use when debugging a
80 /// If there are no errors, the function returns false. If an error is found,
81 /// a message describing the error is written to OS (if non-null) and true is
83 bool verifyFunction(const Function &F, raw_ostream *OS = nullptr);
85 /// \brief Check a module for errors.
87 /// If there are no errors, the function returns false. If an error is
88 /// found, a message describing the error is written to OS (if
89 /// non-null) and true is returned.
91 /// \return true if the module is broken. If BrokenDebugInfo is
92 /// supplied, DebugInfo verification failures won't be considered as
93 /// error and instead *BrokenDebugInfo will be set to true. Debug
94 /// info errors can be "recovered" from by stripping the debug info.
95 bool verifyModule(const Module &M, raw_ostream *OS = nullptr,
96 bool *BrokenDebugInfo = nullptr);
98 FunctionPass *createVerifierPass(bool FatalErrors = true);
100 /// Check a module for errors, and report separate error states for IR
101 /// and debug info errors.
102 class VerifierAnalysis : public AnalysisInfoMixin<VerifierAnalysis> {
103 friend AnalysisInfoMixin<VerifierAnalysis>;
104 static AnalysisKey Key;
108 bool IRBroken, DebugInfoBroken;
110 Result run(Module &M, ModuleAnalysisManager &);
111 Result run(Function &F, FunctionAnalysisManager &);
114 /// Check a module for errors, but report debug info errors separately.
115 /// Otherwise behaves as the normal verifyModule. Debug info errors can be
116 /// "recovered" from by stripping the debug info.
117 bool verifyModule(bool &BrokenDebugInfo, const Module &M, raw_ostream *OS);
119 /// \brief Create a verifier pass.
121 /// Check a module or function for validity. This is essentially a pass wrapped
122 /// around the above verifyFunction and verifyModule routines and
123 /// functionality. When the pass detects a verification error it is always
124 /// printed to stderr, and by default they are fatal. You can override that by
125 /// passing \c false to \p FatalErrors.
127 /// Note that this creates a pass suitable for the legacy pass manager. It has
128 /// nothing to do with \c VerifierPass.
129 class VerifierPass : public PassInfoMixin<VerifierPass> {
133 explicit VerifierPass(bool FatalErrors = true) : FatalErrors(FatalErrors) {}
135 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
136 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
140 } // End llvm namespace