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
16namespace llvm {
17
18class AAResults;
19class ScalarEvolution;
21
22namespace sandboxir {
23
24class Function;
25class Region;
26
27class Analyses {
28 AAResults *AA = nullptr;
29 ScalarEvolution *SE = nullptr;
30 TargetTransformInfo *TTI = nullptr;
31
32 Analyses() = default;
33
34public:
36 : AA(&AA), SE(&SE), TTI(&TTI) {}
37
38public:
39 AAResults &getAA() const { return *AA; }
40 ScalarEvolution &getScalarEvolution() const { return *SE; }
41 TargetTransformInfo &getTTI() const { return *TTI; }
42 /// For use by unit tests.
43 static Analyses emptyForTesting() { return Analyses(); }
44};
45
46/// The base class of a Sandbox IR Pass.
47class Pass {
48protected:
49 /// The pass name. This is also used as a command-line flag and should not
50 /// contain whitespaces.
51 const std::string Name;
52
53public:
54 /// \p Name can't contain any spaces or start with '-'.
56 assert(!Name.contains(' ') &&
57 "A pass name should not contain whitespaces!");
58 assert(!Name.starts_with('-') && "A pass name should not start with '-'!");
59 }
60 virtual ~Pass() = default;
61 /// \Returns the name of the pass.
62 StringRef getName() const { return Name; }
63#ifndef NDEBUG
65 Pass.print(OS);
66 return OS;
67 }
68 virtual void print(raw_ostream &OS) const { OS << Name; }
69 LLVM_ABI LLVM_DUMP_METHOD virtual void dump() const;
70#endif
71 /// Similar to print() but adds a newline. Used for testing.
72 virtual void printPipeline(raw_ostream &OS) const { OS << Name << "\n"; }
73};
74
75/// A pass that runs on a sandbox::Function.
76class FunctionPass : public Pass {
77public:
78 /// \p Name can't contain any spaces or start with '-'.
80 /// \Returns true if it modifies \p F.
81 virtual bool runOnFunction(Function &F, const Analyses &A) = 0;
82};
83
84/// A pass that runs on a sandbox::Region.
85class RegionPass : public Pass {
86public:
87 /// \p Name can't contain any spaces or start with '-'.
89 /// \Returns true if it modifies \p R.
90 virtual bool runOnRegion(Region &R, const Analyses &A) = 0;
91};
92
94/// This represents an auxiliary pass argument. Its value defaults to false and
95/// gets set by AuxPassArgsRegistry::parse(). It's value is cheap to access.
96class AuxPassArg {
97 unsigned ArgIdx = 0;
98 AuxPassArgsRegistry *Registry = nullptr;
99
100 /// Use AuxPassArgsRegistry::createArg() to create.
101 AuxPassArg() = delete;
102 AuxPassArg(unsigned ArgIdx, AuxPassArgsRegistry *Registry)
103 : ArgIdx(ArgIdx), Registry(Registry) {}
104 friend class AuxPassArgsRegistry; // For constructor.
105 bool set(bool NewVal);
106
107public:
108 bool get() const;
109 StringRef getFlagStr() const;
110 operator bool() const { return get(); }
111 bool operator=(bool NewVal) { return set(NewVal); }
112
113#ifndef NDEBUG
114 void print(raw_ostream &OS) const;
115 LLVM_DUMP_METHOD void dump() const;
116#endif
117};
118
119/// A registry for the auxiliary pass argument arguments. This includes a
120/// builder for the pass arguments.
122 struct Entry {
123 Entry(StringRef FlagStr, bool Val) : FlagStr(FlagStr), Val(Val) {}
124 StringRef FlagStr;
125 bool Val;
126 };
127 SmallVector<Entry> Entries;
128 friend class AuxPassArg; // For Entries
129
130 /// Linear-time access to the entry for \p Flag. \returns nullptr if \p Flag
131 /// is not registered.
132 Entry *getEntry(StringRef Flag);
133
134public:
135 /// Builder for argument with \p Flag.
137 /// Parse the aux argument string \p ArgsStr and set the values.
138 void parse(StringRef ArgsStr);
139
140#ifndef NDEBUG
141 void print(raw_ostream &OS) const;
142 LLVM_DUMP_METHOD void dump() const;
143#endif
144};
145
146} // namespace sandboxir
147} // namespace llvm
148
149#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:215
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:678
#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:40
Analyses(AAResults &AA, ScalarEvolution &SE, TargetTransformInfo &TTI)
Definition Pass.h:35
AAResults & getAA() const
Definition Pass.h:39
TargetTransformInfo & getTTI() const
Definition Pass.h:41
static Analyses emptyForTesting()
For use by unit tests.
Definition Pass.h:43
StringRef getFlagStr() const
Definition Pass.cpp:28
LLVM_DUMP_METHOD void dump() const
Definition Pass.cpp:38
void print(raw_ostream &OS) const
Definition Pass.cpp:33
friend class AuxPassArgsRegistry
Definition Pass.h:104
bool operator=(bool NewVal)
Definition Pass.h:111
A registry for the auxiliary pass argument arguments.
Definition Pass.h:121
void print(raw_ostream &OS) const
Definition Pass.cpp:79
AuxPassArg createArg(StringRef Flag)
Builder for argument with Flag.
Definition Pass.cpp:52
void parse(StringRef ArgsStr)
Parse the aux argument string ArgsStr and set the values.
Definition Pass.cpp:58
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:79
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:72
const std::string Name
The pass name.
Definition Pass.h:51
LLVM_ABI virtual LLVM_DUMP_METHOD void dump() const
friend raw_ostream & operator<<(raw_ostream &OS, const Pass &Pass)
Definition Pass.h:64
Pass(StringRef Name)
Name can't contain any spaces or start with '-'.
Definition Pass.h:55
virtual void print(raw_ostream &OS) const
Definition Pass.h:68
StringRef getName() const
\Returns the name of the pass.
Definition Pass.h:62
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:88
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.