]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/optimize.h
Mark function return types as used
[libs/gl.git] / source / glsl / optimize.h
1 #ifndef MSP_GL_SL_OPTIMIZE_H_
2 #define MSP_GL_SL_OPTIMIZE_H_
3
4 #include <map>
5 #include <set>
6 #include "evaluate.h"
7 #include "visitor.h"
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 /** Finds functions which are candidates for inlining.  Currently this means
14 functions which have no parameters, contain no more than one return statement,
15 and are only called once. */
16 class InlineableFunctionLocator: private TraversingVisitor
17 {
18 private:
19         std::map<FunctionDeclaration *, unsigned> refcounts;
20         std::set<FunctionDeclaration *> inlineable;
21         FunctionDeclaration *current_function;
22         unsigned return_count;
23
24 public:
25         InlineableFunctionLocator();
26
27         const std::set<FunctionDeclaration *> &apply(Stage &s) { s.content.visit(*this); return inlineable; }
28
29 private:
30         virtual void visit(FunctionCall &);
31         virtual void visit(FunctionDeclaration &);
32         virtual void visit(Conditional &);
33         virtual void visit(Iteration &);
34         virtual void visit(Return &);
35 };
36
37 /** Injects statements from one function into another.  Local variables are
38 renamed to avoid conflicts.  After inlining, uses NodeReorderer to cause
39 dependencies of the inlined statements to appear before the target function. */
40 class InlineContentInjector: private TraversingVisitor
41 {
42 private:
43         FunctionDeclaration *source_func;
44         Block *target_block;
45         std::map<std::string, VariableDeclaration *> variable_map;
46         bool remap_names;
47         bool deps_only;
48         RefPtr<Statement> r_inlined_statement;
49         std::set<Node *> dependencies;
50         std::string r_result_name;
51
52 public:
53         InlineContentInjector();
54
55         const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionDeclaration &);
56
57 private:
58         std::string create_unused_name(const std::string &, bool);
59
60         virtual void visit(VariableReference &);
61         virtual void visit(InterfaceBlockReference &);
62         virtual void visit(FunctionCall &);
63         virtual void visit(VariableDeclaration &);
64         virtual void visit(Return &);
65 };
66
67 /** Inlines functions.  Internally uses InlineableFunctionLocator to find
68 candidate functions.  Only functions which consist of a single return statement
69 are inlined. */
70 class FunctionInliner: private TraversingVisitor
71 {
72 private:
73         Stage *stage;
74         std::set<FunctionDeclaration *> inlineable;
75         FunctionDeclaration *current_function;
76         NodeList<Statement>::iterator insert_point;
77         RefPtr<Expression> r_inline_result;
78         bool r_any_inlined;
79
80 public:
81         FunctionInliner();
82
83         bool apply(Stage &);
84
85 private:
86         void visit_and_inline(RefPtr<Expression> &);
87
88         virtual void visit(Block &);
89         virtual void visit(UnaryExpression &);
90         virtual void visit(BinaryExpression &);
91         virtual void visit(MemberAccess &);
92         virtual void visit(FunctionCall &);
93         virtual void visit(ExpressionStatement &);
94         virtual void visit(VariableDeclaration &);
95         virtual void visit(FunctionDeclaration &);
96         virtual void visit(Conditional &);
97         virtual void visit(Iteration &);
98         virtual void visit(Return &);
99 };
100
101 /** Inlines variables into expressions.  Variables with trivial values (those
102 consisting of a single literal or variable reference) are always inlined.
103 Variables which are only referenced once are also inlined. */
104 class ExpressionInliner: private TraversingVisitor
105 {
106 private:
107         struct ExpressionInfo
108         {
109                 Expression *expression;
110                 Block *assign_scope;
111                 RefPtr<Expression> *inline_point;
112                 const Operator *inner_oper;
113                 const Operator *outer_oper;
114                 bool inline_on_rhs;
115                 bool trivial;
116                 bool available;
117
118                 ExpressionInfo();
119         };
120
121         std::map<VariableDeclaration *, ExpressionInfo> expressions;
122         ExpressionInfo *r_ref_info;
123         bool r_any_inlined;
124         bool r_trivial;
125         bool mutating;
126         bool iteration_init;
127         Block *iteration_body;
128         const Operator *r_oper;
129
130 public:
131         ExpressionInliner();
132
133         bool apply(Stage &);
134
135 private:
136         void visit_and_record(RefPtr<Expression> &, const Operator *, bool);
137         void inline_expression(Expression &, RefPtr<Expression> &, const Operator *, const Operator *, bool);
138         virtual void visit(Block &);
139         virtual void visit(VariableReference &);
140         virtual void visit(MemberAccess &);
141         virtual void visit(UnaryExpression &);
142         virtual void visit(BinaryExpression &);
143         virtual void visit(Assignment &);
144         virtual void visit(FunctionCall &);
145         virtual void visit(VariableDeclaration &);
146         virtual void visit(Conditional &);
147         virtual void visit(Iteration &);
148         virtual void visit(Return &);
149 };
150
151 /** Removes conditional statements and loops where the condition can be
152 determined as constant at compile time. */
153 class ConstantConditionEliminator: private TraversingVisitor
154 {
155 private:
156         NodeList<Statement>::iterator insert_point;
157         std::set<Node *> nodes_to_remove;
158
159 public:
160         void apply(Stage &);
161
162 private:
163         virtual void visit(Block &);
164         virtual void visit(Conditional &);
165         virtual void visit(Iteration &);
166 };
167
168 /** Removes types which are not used anywhere. */
169 class UnusedTypeRemover: private TraversingVisitor
170 {
171 private:
172         std::set<Node *> unused_nodes;
173
174 public:
175         bool apply(Stage &);
176
177 private:
178         virtual void visit(BasicTypeDeclaration &);
179         virtual void visit(ImageTypeDeclaration &);
180         virtual void visit(StructDeclaration &);
181         virtual void visit(VariableDeclaration &);
182         virtual void visit(FunctionDeclaration &);
183 };
184
185 /** Removes variable declarations with no references to them.  Assignment
186 statements where the result is not used are also removed. */
187 class UnusedVariableRemover: private TraversingVisitor
188 {
189 private:
190         struct VariableInfo
191         {
192                 bool local;
193                 std::vector<Node *> assignments;
194                 bool conditionally_assigned;
195                 bool referenced;
196
197                 VariableInfo();
198         };
199
200         typedef std::map<VariableDeclaration *, VariableInfo> BlockVariableMap;
201
202         std::set<Node *> unused_nodes;
203         std::map<VariableDeclaration *, Node *> aggregates;
204         Node *aggregate;
205         std::vector<BlockVariableMap> variables;
206         Assignment *r_assignment;
207         bool assignment_target;
208         bool r_assign_to_subfield;
209         bool r_side_effects;
210
211 public:
212         UnusedVariableRemover();
213
214         bool apply(Stage &);
215
216 private:
217         virtual void visit(VariableReference &);
218         virtual void visit(InterfaceBlockReference &);
219         virtual void visit(MemberAccess &);
220         virtual void visit(UnaryExpression &);
221         virtual void visit(BinaryExpression &);
222         virtual void visit(Assignment &);
223         void record_assignment(VariableDeclaration &, Node &, bool);
224         void clear_assignments(VariableInfo &, bool);
225         virtual void visit(FunctionCall &);
226         virtual void visit(ExpressionStatement &);
227         virtual void visit(StructDeclaration &);
228         virtual void visit(VariableDeclaration &);
229         virtual void visit(InterfaceBlock &);
230         virtual void visit(FunctionDeclaration &);
231         void merge_down_variables();
232         virtual void visit(Conditional &);
233         virtual void visit(Iteration &);
234 };
235
236 /** Removes function declarations with no references to them. */
237 class UnusedFunctionRemover: private TraversingVisitor
238 {
239 private:
240         std::set<Node *> unused_nodes;
241         std::set<FunctionDeclaration *> used_definitions;
242
243 public:
244         bool apply(Stage &s);
245
246 private:
247         virtual void visit(FunctionCall &);
248         virtual void visit(FunctionDeclaration &);
249 };
250
251 } // namespace SL
252 } // namespace GL
253 } // namespace Msp
254
255 #endif