/src/hermes/include/hermes/Optimizer/PassManager/Pass.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | #ifndef HERMES_OPTIMIZER_PASSMANAGER_PASS_H |
9 | | #define HERMES_OPTIMIZER_PASSMANAGER_PASS_H |
10 | | |
11 | | #include "llvh/ADT/StringRef.h" |
12 | | |
13 | | #include <memory> |
14 | | |
15 | | namespace hermes { |
16 | | |
17 | | class Function; |
18 | | class Module; |
19 | | class Instruction; |
20 | | class IRBuilder; |
21 | | |
22 | | /// This class represents a pass, which is a transformation of the IR. Passes |
23 | | /// are either Function passes, which transform one function, and are not |
24 | | /// allowed to create new functions or modify other functions, or module |
25 | | /// passes which operate on the entire module and are free to manipulate |
26 | | /// multiple functions. |
27 | | class Pass { |
28 | | public: |
29 | | enum class PassKind { |
30 | | Function, |
31 | | Module, |
32 | | }; |
33 | | |
34 | | private: |
35 | | /// Stores the kind of derived class. |
36 | | const PassKind kind; |
37 | | /// The textual name of the pass. |
38 | | llvh::StringRef name; |
39 | | |
40 | | public: |
41 | | /// Constructor. \p K indicates the kind of pass this is. |
42 | 71.4k | explicit Pass(Pass::PassKind K, llvh::StringRef name) : kind(K), name(name) {} |
43 | | |
44 | 71.4k | virtual ~Pass() = default; |
45 | | |
46 | | /// Returns the kind of the pass. |
47 | 142k | PassKind getKind() const { |
48 | 142k | return kind; |
49 | 142k | } |
50 | | |
51 | | /// Returns the textual name of the pass. |
52 | 142k | llvh::StringRef getName() const { |
53 | 142k | return name; |
54 | 142k | } |
55 | | }; |
56 | | |
57 | | class FunctionPass : public Pass { |
58 | | public: |
59 | | explicit FunctionPass(llvh::StringRef name) |
60 | 71.4k | : Pass(Pass::PassKind::Function, name) {} |
61 | | ~FunctionPass() override = default; |
62 | | |
63 | | /// Runs the current pass on the function \p F. |
64 | | /// \returns true if the function was modified. |
65 | | virtual bool runOnFunction(Function *F) = 0; |
66 | | |
67 | 142k | static bool classof(const Pass *S) { |
68 | 142k | return S->getKind() == PassKind::Function; |
69 | 142k | } |
70 | | }; |
71 | | |
72 | | class ModulePass : public Pass { |
73 | | public: |
74 | | explicit ModulePass(llvh::StringRef name) |
75 | 0 | : Pass(Pass::PassKind::Module, name) {} |
76 | | ~ModulePass() override = default; |
77 | | |
78 | | /// Runs the current pass on the module \p M. |
79 | | /// \returns true if module was modified. |
80 | | virtual bool runOnModule(Module *M) = 0; |
81 | | |
82 | 0 | static bool classof(const Pass *S) { |
83 | 0 | return S->getKind() == PassKind::Module; |
84 | 0 | } |
85 | | }; |
86 | | |
87 | | /// Pass header declaration. |
88 | | #define PASS(ID, NAME, DESCRIPTION) std::unique_ptr<Pass> create##ID(); |
89 | | #include "Passes.def" |
90 | | |
91 | | } // namespace hermes |
92 | | |
93 | | #endif // HERMES_OPTIMIZER_PASSMANAGER_PASS_H |