]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/optimize.h
Adjust member access of the various visitors in 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 class InlineableFunctionLocator: private TraversingVisitor
14 {
15 private:
16         std::map<FunctionDeclaration *, unsigned> refcounts;
17         std::set<FunctionDeclaration *> inlineable;
18         FunctionDeclaration *in_function;
19
20 public:
21         InlineableFunctionLocator();
22
23         const std::set<FunctionDeclaration *> &apply(Stage &s) { visit(s.content); return inlineable; }
24
25 private:
26         virtual void visit(FunctionCall &);
27         virtual void visit(FunctionDeclaration &);
28         using TraversingVisitor::visit;
29 };
30
31 class FunctionInliner: private TraversingVisitor
32 {
33 private:
34         std::set<FunctionDeclaration *> inlineable;
35         unsigned extract_result;
36         RefPtr<Expression> inline_result;
37
38 public:
39         FunctionInliner();
40         FunctionInliner(const std::set<FunctionDeclaration *> &);
41
42         void apply(Stage &s) { visit(s.content); }
43
44 private:
45         void visit_and_inline(RefPtr<Expression> &);
46
47         virtual void visit(Block &);
48         virtual void visit(UnaryExpression &);
49         virtual void visit(BinaryExpression &);
50         virtual void visit(MemberAccess &);
51         virtual void visit(FunctionCall &);
52         virtual void visit(VariableDeclaration &);
53         virtual void visit(Return &);
54         using TraversingVisitor::visit;
55 };
56
57 class ConstantConditionEliminator: private BlockModifier
58 {
59 private:
60         unsigned scope_level;
61         bool record_only;
62         ExpressionEvaluator::ValueMap variable_values;
63
64 public:
65         ConstantConditionEliminator();
66
67         void apply(Stage &s) { visit(s.content); }
68
69 private:
70         virtual void visit(Block &);
71         virtual void visit(UnaryExpression &);
72         virtual void visit(Assignment &);
73         virtual void visit(VariableDeclaration &);
74         virtual void visit(Conditional &);
75         virtual void visit(Iteration &);
76         using BlockModifier::visit;
77 };
78
79 class UnusedVariableLocator: private TraversingVisitor
80 {
81 private:
82         struct VariableInfo
83         {
84                 bool local;
85                 std::vector<Node *> assignments;
86                 bool conditionally_assigned;
87                 bool referenced;
88
89                 VariableInfo();
90         };
91
92         typedef std::map<VariableDeclaration *, VariableInfo> BlockVariableMap;
93
94         std::set<Node *> unused_nodes;
95         std::map<VariableDeclaration *, Node *> aggregates;
96         Node *aggregate;
97         std::vector<BlockVariableMap> variables;
98         Assignment *assignment;
99         bool assignment_target;
100         bool assign_to_subscript;
101         bool global_scope;
102
103 public:
104         UnusedVariableLocator();
105
106         const std::set<Node *> &apply(Stage &);
107
108 private:
109         virtual void visit(VariableReference &);
110         virtual void visit(MemberAccess &);
111         virtual void visit(BinaryExpression &);
112         virtual void visit(Assignment &);
113         void record_assignment(VariableDeclaration &, Node &, bool);
114         void clear_assignments(VariableInfo &, bool);
115         virtual void visit(ExpressionStatement &);
116         virtual void visit(StructDeclaration &);
117         virtual void visit(VariableDeclaration &);
118         virtual void visit(InterfaceBlock &);
119         virtual void visit(FunctionDeclaration &);
120         void merge_down_variables();
121         virtual void visit(Conditional &);
122         virtual void visit(Iteration &);
123         using TraversingVisitor::visit;
124 };
125
126 class UnusedFunctionLocator: private TraversingVisitor
127 {
128 private:
129         std::set<Node *> unused_nodes;
130         std::set<FunctionDeclaration *> used_definitions;
131
132 public:
133         const std::set<Node *> &apply(Stage &s) { visit(s.content); return unused_nodes; }
134
135 private:
136         virtual void visit(FunctionCall &);
137         virtual void visit(FunctionDeclaration &);
138         using TraversingVisitor::visit;
139 };
140
141 } // namespace SL
142 } // namespace GL
143 } // namespace Msp
144
145 #endif