LLVM 22.0.0git
Verifier.h
Go to the documentation of this file.
1//===- Verifier.h - LLVM IR Verifier ----------------------------*- C++ -*-===//
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
10// validation checking of input to the system, and for checking that
11// transformations haven't done something bad.
12//
13// Note that this does not provide full 'java style' security and verifications,
14// instead it just tries to ensure that code is well formed.
15//
16// To see what specifically is checked, look at the top of Verifier.cpp
17//
18//===----------------------------------------------------------------------===//
19
20#ifndef LLVM_IR_VERIFIER_H
21#define LLVM_IR_VERIFIER_H
22
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/IR/PassManager.h"
26#include <utility>
27
28namespace llvm {
29
30class APInt;
31class Function;
32class FunctionPass;
33class Instruction;
34class MDNode;
35class Module;
36class raw_ostream;
37struct VerifierSupport;
38
39/// Verify that the TBAA Metadatas are valid.
41 VerifierSupport *Diagnostic = nullptr;
42
43 /// Helper to diagnose a failure
44 template <typename... Tys> void CheckFailed(Tys &&... Args);
45
46 /// Cache of TBAA base nodes that have already been visited. This cachce maps
47 /// a node that has been visited to a pair (IsInvalid, BitWidth) where
48 ///
49 /// \c IsInvalid is true iff the node is invalid.
50 /// \c BitWidth, if non-zero, is the bitwidth of the integer used to denoting
51 /// the offset of the access. If zero, only a zero offset is allowed.
52 ///
53 /// \c BitWidth has no meaning if \c IsInvalid is true.
54 using TBAABaseNodeSummary = std::pair<bool, unsigned>;
56
57 /// Maps an alleged scalar TBAA node to a boolean that is true if the said
58 /// TBAA node is a valid scalar TBAA node or false otherwise.
59 DenseMap<const MDNode *, bool> TBAAScalarNodes;
60
61 /// \name Helper functions used by \c visitTBAAMetadata.
62 /// @{
63 MDNode *getFieldNodeFromTBAABaseNode(const Instruction *I,
64 const MDNode *BaseNode, APInt &Offset,
65 bool IsNewFormat);
66 TBAAVerifier::TBAABaseNodeSummary verifyTBAABaseNode(const Instruction *I,
67 const MDNode *BaseNode,
68 bool IsNewFormat);
69 TBAABaseNodeSummary verifyTBAABaseNodeImpl(const Instruction *I,
70 const MDNode *BaseNode,
71 bool IsNewFormat);
72
73 bool isValidScalarTBAANode(const MDNode *MD);
74 /// @}
75
76public:
77 TBAAVerifier(VerifierSupport *Diagnostic = nullptr)
78 : Diagnostic(Diagnostic) {}
79 /// Visit an instruction, or a TBAA node itself as part of a metadata, and
80 /// return true if it is valid, return false if an invalid TBAA is attached.
81 LLVM_ABI bool visitTBAAMetadata(const Instruction *I, const MDNode *MD);
82};
83
84/// Check a function for errors, useful for use when debugging a
85/// pass.
86///
87/// If there are no errors, the function returns false. If an error is found,
88/// a message describing the error is written to OS (if non-null) and true is
89/// returned.
90LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS = nullptr);
91
92/// Check a module for errors.
93///
94/// If there are no errors, the function returns false. If an error is
95/// found, a message describing the error is written to OS (if
96/// non-null) and true is returned.
97///
98/// \return true if the module is broken. If BrokenDebugInfo is
99/// supplied, DebugInfo verification failures won't be considered as
100/// error and instead *BrokenDebugInfo will be set to true. Debug
101/// info errors can be "recovered" from by stripping the debug info.
102LLVM_ABI bool verifyModule(const Module &M, raw_ostream *OS = nullptr,
103 bool *BrokenDebugInfo = nullptr);
104
105LLVM_ABI FunctionPass *createVerifierPass(bool FatalErrors = true);
106
107/// Check a module for errors, and report separate error states for IR
108/// and debug info errors.
109class VerifierAnalysis : public AnalysisInfoMixin<VerifierAnalysis> {
111
112 LLVM_ABI static AnalysisKey Key;
113
114public:
115 struct Result {
117 };
118
121 static bool isRequired() { return true; }
122};
123
124/// Create a verifier pass.
125///
126/// Check a module or function for validity. This is essentially a pass wrapped
127/// around the above verifyFunction and verifyModule routines and
128/// functionality. When the pass detects a verification error it is always
129/// printed to stderr, and by default they are fatal. You can override that by
130/// passing \c false to \p FatalErrors.
131///
132/// Note that this creates a pass suitable for the legacy pass manager. It has
133/// nothing to do with \c VerifierPass.
134class VerifierPass : public PassInfoMixin<VerifierPass> {
135 bool FatalErrors;
136
137public:
138 explicit VerifierPass(bool FatalErrors = true) : FatalErrors(FatalErrors) {}
139
142 static bool isRequired() { return true; }
143};
144
145} // end namespace llvm
146
147#endif // LLVM_IR_VERIFIER_H
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
Class for arbitrary precision integers.
Definition APInt.h:78
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Metadata node.
Definition Metadata.h:1078
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
LLVM_ABI bool visitTBAAMetadata(const Instruction *I, const MDNode *MD)
Visit an instruction, or a TBAA node itself as part of a metadata, and return true if it is valid,...
TBAAVerifier(VerifierSupport *Diagnostic=nullptr)
Definition Verifier.h:77
Check a module for errors, and report separate error states for IR and debug info errors.
Definition Verifier.h:109
static bool isRequired()
Definition Verifier.h:121
LLVM_ABI Result run(Module &M, ModuleAnalysisManager &)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
static bool isRequired()
Definition Verifier.h:142
VerifierPass(bool FatalErrors=true)
Definition Verifier.h:138
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
LLVM_ABI FunctionPass * createVerifierPass(bool FatalErrors=true)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70