LLVM 24.0.0git
Pass.h
Go to the documentation of this file.
1//===- Pass.h ---------------------------------------------------*- 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#ifndef LLVM_SANDBOXIR_PASS_H
10#define LLVM_SANDBOXIR_PASS_H
11
12#include "llvm/ADT/StringRef.h"
15
16class PassManagerTest_FunctionPassReturnValue_Test;
17class PassManagerTest_RegionPassReturnValue_Test;
18
19namespace llvm {
20
21class AAResults;
22class ScalarEvolution;
24
25namespace sandboxir {
26
27class Function;
28class Region;
29
30class Analyses {
31 AAResults *AA = nullptr;
32 ScalarEvolution *SE = nullptr;
33 TargetTransformInfo *TTI = nullptr;
34
35 Analyses() = default;
36 // Friends for default constructor in tests.
37 friend class ::PassManagerTest_FunctionPassReturnValue_Test;
38 friend class ::PassManagerTest_RegionPassReturnValue_Test;
39
40public:
42 : AA(&AA), SE(&SE), TTI(&TTI) {}
43
44public:
45 AAResults &getAA() const { return *AA; }
46 ScalarEvolution &getScalarEvolution() const { return *SE; }
47 TargetTransformInfo &getTTI() const { return *TTI; }
48 /// For use by unit tests.
49 static Analyses emptyForTesting() { return Analyses(); }
50};
51
52/// The base class of a Sandbox IR Pass.
54protected:
55 /// The pass name. This is also used as a command-line flag and should not
56 /// contain whitespaces.
57 const std::string Name;
58
59public:
60 /// \p Name can't contain any spaces or start with '-'.
62 assert(!Name.contains(' ') &&
63 "A pass name should not contain whitespaces!");
64 assert(!Name.starts_with('-') && "A pass name should not start with '-'!");
65 }
66 virtual ~Pass() = default;
67 /// \Returns the name of the pass.
68 StringRef getName() const { return Name; }
69#ifndef NDEBUG
71 Pass.print(OS);
72 return OS;
73 }
74 virtual void print(raw_ostream &OS) const { OS << Name; }
75 LLVM_DUMP_METHOD virtual void dump() const;
76#endif
77 /// Similar to print() but adds a newline. Used for testing.
78 virtual void printPipeline(raw_ostream &OS) const { OS << Name << "\n"; }
79};
80
81/// A pass that runs on a sandbox::Function.
82class FunctionPass : public Pass {
83public:
84 /// \p Name can't contain any spaces or start with '-'.
86 /// \Returns true if it modifies \p F.
87 virtual bool runOnFunction(Function &F, const Analyses &A) = 0;
88};
89
90/// A pass that runs on a sandbox::Region.
91class RegionPass : public Pass {
92public:
93 /// \p Name can't contain any spaces or start with '-'.
95 /// \Returns true if it modifies \p R.
96 virtual bool runOnRegion(Region &R, const Analyses &A) = 0;
97};
98
100/// This represents an auxiliary pass argument. Its value defaults to false and
101/// gets set by AuxPassArgsRegistry::parse(). It's value is cheap to access.
102class AuxPassArg {
103 unsigned ArgIdx = 0;
104 AuxPassArgsRegistry *Registry = nullptr;
105
106 /// Use AuxPassArgsRegistry::createArg() to create.
107 AuxPassArg() = delete;
108 AuxPassArg(unsigned ArgIdx, AuxPassArgsRegistry *Registry)
109 : ArgIdx(ArgIdx), Registry(Registry) {}
110 friend class AuxPassArgsRegistry; // For constructor.
111 LLVM_ABI bool set(bool NewVal);
112
113public:
114 LLVM_ABI bool get() const;
116 operator bool() const { return get(); }
117 bool operator=(bool NewVal) { return set(NewVal); }
118
119#ifndef NDEBUG
120 LLVM_ABI void print(raw_ostream &OS) const;
121 LLVM_ABI LLVM_DUMP_METHOD void dump() const;
122#endif
123};
124
125/// A registry for the auxiliary pass argument arguments. This includes a
126/// builder for the pass arguments.
128 struct Entry {
129 Entry(StringRef FlagStr, bool Val) : FlagStr(FlagStr), Val(Val) {}
130 StringRef FlagStr;
131 bool Val;
132 };
133 SmallVector<Entry> Entries;
134 friend class AuxPassArg; // For Entries
135
136 /// Linear-time access to the entry for \p Flag. \returns nullptr if \p Flag
137 /// is not registered.
138 Entry *getEntry(StringRef Flag);
139
140public:
141 /// Builder for argument with \p Flag.
143 /// Parse the aux argument string \p ArgsStr and set the values.
144 LLVM_ABI void parse(StringRef ArgsStr);
145
146#ifndef NDEBUG
147 LLVM_ABI void print(raw_ostream &OS) const;
148 LLVM_ABI LLVM_DUMP_METHOD void dump() const;
149#endif
150};
151
152} // namespace sandboxir
153} // namespace llvm
154
155#endif // LLVM_SANDBOXIR_PASS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:702
#define F(x, y, z)
Definition MD5.cpp:54
The main scalar evolution driver.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
ScalarEvolution & getScalarEvolution() const
Definition Pass.h:46
Analyses(AAResults &AA, ScalarEvolution &SE, TargetTransformInfo &TTI)
Definition Pass.h:41
AAResults & getAA() const
Definition Pass.h:45
TargetTransformInfo & getTTI() const
Definition Pass.h:47
static Analyses emptyForTesting()
For use by unit tests.
Definition Pass.h:49
LLVM_ABI StringRef getFlagStr() const
Definition Pass.cpp:28
LLVM_ABI LLVM_DUMP_METHOD void dump() const
Definition Pass.cpp:38
LLVM_ABI void print(raw_ostream &OS) const
Definition Pass.cpp:33
friend class AuxPassArgsRegistry
Definition Pass.h:110
bool operator=(bool NewVal)
Definition Pass.h:117
LLVM_ABI bool get() const
Definition Pass.cpp:26
A registry for the auxiliary pass argument arguments.
Definition Pass.h:127
LLVM_ABI void print(raw_ostream &OS) const
Definition Pass.cpp:79
LLVM_ABI AuxPassArg createArg(StringRef Flag)
Builder for argument with Flag.
Definition Pass.cpp:52
LLVM_ABI void parse(StringRef ArgsStr)
Parse the aux argument string ArgsStr and set the values.
Definition Pass.cpp:58
LLVM_ABI LLVM_DUMP_METHOD void dump() const
Definition Pass.cpp:88
FunctionPass(StringRef Name)
Name can't contain any spaces or start with '-'.
Definition Pass.h:85
virtual bool runOnFunction(Function &F, const Analyses &A)=0
\Returns true if it modifies F.
virtual ~Pass()=default
virtual void printPipeline(raw_ostream &OS) const
Similar to print() but adds a newline. Used for testing.
Definition Pass.h:78
const std::string Name
The pass name.
Definition Pass.h:57
friend raw_ostream & operator<<(raw_ostream &OS, const Pass &Pass)
Definition Pass.h:70
Pass(StringRef Name)
Name can't contain any spaces or start with '-'.
Definition Pass.h:61
virtual LLVM_DUMP_METHOD void dump() const
virtual void print(raw_ostream &OS) const
Definition Pass.h:74
StringRef getName() const
\Returns the name of the pass.
Definition Pass.h:68
virtual bool runOnRegion(Region &R, const Analyses &A)=0
\Returns true if it modifies R.
RegionPass(StringRef Name)
Name can't contain any spaces or start with '-'.
Definition Pass.h:94
This is an optimization pass for GlobalISel generic memory operations.
MachinePassRegistry< typename RegisterRegAllocBase< T >::FunctionPassCtor > RegisterRegAllocBase< T >::Registry
RegisterRegAlloc's global Registry tracks allocator registration.