]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/optimize.h
Add some documentation to the GLSL compiler
[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, are only called once, and that call occurs
15 after the definition of the function. */
16 class InlineableFunctionLocator: private TraversingVisitor
17 {
18 private:
19         std::map<FunctionDeclaration *, unsigned> refcounts;
20         std::set<FunctionDeclaration *> inlineable;
21         FunctionDeclaration *in_function;
22
23 public:
24         InlineableFunctionLocator();
25
26         const std::set<FunctionDeclaration *> &apply(Stage &s) { s.content.visit(*this); return inlineable; }
27
28 private:
29         virtual void visit(FunctionCall &);
30         virtual void visit(FunctionDeclaration &);
31 };
32
33 /** Inlines functions.  Internally uses InlineableFunctionLocator to find
34 candidate functions.  Only functions which consist of a single return statement
35 are inlined. */
36 class FunctionInliner: private TraversingVisitor
37 {
38 private:
39         std::set<FunctionDeclaration *> inlineable;
40         unsigned extract_result;
41         RefPtr<Expression> inline_result;
42
43 public:
44         FunctionInliner();
45
46         void apply(Stage &);
47
48 private:
49         void visit_and_inline(RefPtr<Expression> &);
50
51         virtual void visit(Block &);
52         virtual void visit(UnaryExpression &);
53         virtual void visit(BinaryExpression &);
54         virtual void visit(MemberAccess &);
55         virtual void visit(FunctionCall &);
56         virtual void visit(VariableDeclaration &);
57         virtual void visit(Return &);
58 };
59
60 /** Removes conditional statements and loops where the condition can be
61 determined as constant at compile time. */
62 class ConstantConditionEliminator: private TraversingVisitor
63 {
64 private:
65         bool record_only;
66         ExpressionEvaluator::ValueMap variable_values;
67         NodeList<Statement>::iterator insert_point;
68         std::set<Node *> nodes_to_remove;
69
70 public:
71         ConstantConditionEliminator();
72
73         void apply(Stage &);
74
75 private:
76         virtual void visit(Block &);
77         virtual void visit(UnaryExpression &);
78         virtual void visit(Assignment &);
79         virtual void visit(VariableDeclaration &);
80         virtual void visit(Conditional &);
81         virtual void visit(Iteration &);
82 };
83
84 /** Removes variable declarations with no references to them.  Assignment
85 statements where the result is not used are also removed. */
86 class UnusedVariableRemover: private TraversingVisitor
87 {
88 private:
89         struct VariableInfo
90         {
91                 bool local;
92                 std::vector<Node *> assignments;
93                 bool conditionally_assigned;
94                 bool referenced;
95
96                 VariableInfo();
97         };
98
99         typedef std::map<VariableDeclaration *, VariableInfo> BlockVariableMap;
100
101         std::set<Node *> unused_nodes;
102         std::map<VariableDeclaration *, Node *> aggregates;
103         Node *aggregate;
104         std::vector<BlockVariableMap> variables;
105         Assignment *assignment;
106         bool assignment_target;
107         bool assign_to_subscript;
108
109 public:
110         UnusedVariableRemover();
111
112         bool apply(Stage &);
113
114 private:
115         virtual void visit(VariableReference &);
116         virtual void visit(InterfaceBlockReference &);
117         virtual void visit(MemberAccess &);
118         virtual void visit(BinaryExpression &);
119         virtual void visit(Assignment &);
120         void record_assignment(VariableDeclaration &, Node &, bool);
121         void clear_assignments(VariableInfo &, bool);
122         virtual void visit(ExpressionStatement &);
123         virtual void visit(StructDeclaration &);
124         virtual void visit(VariableDeclaration &);
125         virtual void visit(InterfaceBlock &);
126         virtual void visit(FunctionDeclaration &);
127         void merge_down_variables();
128         virtual void visit(Conditional &);
129         virtual void visit(Iteration &);
130 };
131
132 /** Removes function declarations with no references to them. */
133 class UnusedFunctionRemover: private TraversingVisitor
134 {
135 private:
136         std::set<Node *> unused_nodes;
137         std::set<FunctionDeclaration *> used_definitions;
138
139 public:
140         bool apply(Stage &s);
141
142 private:
143         virtual void visit(FunctionCall &);
144         virtual void visit(FunctionDeclaration &);
145 };
146
147 } // namespace SL
148 } // namespace GL
149 } // namespace Msp
150
151 #endif