]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.h
Add some documentation to the GLSL compiler
[libs/gl.git] / source / glsl / optimize.h
index 2d1b20c99e72d7b1361508cc9bdda14c2424952b..ed016ca0b9ddd26dd5e8e871b25a0f15484e2881 100644 (file)
@@ -10,6 +10,9 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+/** Finds functions which are candidates for inlining.  Currently this means
+functions which have no parameters, are only called once, and that call occurs
+after the definition of the function. */
 class InlineableFunctionLocator: private TraversingVisitor
 {
 private:
@@ -20,14 +23,16 @@ private:
 public:
        InlineableFunctionLocator();
 
-       const std::set<FunctionDeclaration *> &apply(Stage &s) { visit(s.content); return inlineable; }
+       const std::set<FunctionDeclaration *> &apply(Stage &s) { s.content.visit(*this); return inlineable; }
 
 private:
        virtual void visit(FunctionCall &);
        virtual void visit(FunctionDeclaration &);
-       using TraversingVisitor::visit;
 };
 
+/** Inlines functions.  Internally uses InlineableFunctionLocator to find
+candidate functions.  Only functions which consist of a single return statement
+are inlined. */
 class FunctionInliner: private TraversingVisitor
 {
 private:
@@ -50,20 +55,22 @@ private:
        virtual void visit(FunctionCall &);
        virtual void visit(VariableDeclaration &);
        virtual void visit(Return &);
-       using TraversingVisitor::visit;
 };
 
-class ConstantConditionEliminator: private BlockModifier
+/** Removes conditional statements and loops where the condition can be
+determined as constant at compile time. */
+class ConstantConditionEliminator: private TraversingVisitor
 {
 private:
-       unsigned scope_level;
        bool record_only;
        ExpressionEvaluator::ValueMap variable_values;
+       NodeList<Statement>::iterator insert_point;
+       std::set<Node *> nodes_to_remove;
 
 public:
        ConstantConditionEliminator();
 
-       void apply(Stage &s) { visit(s.content); }
+       void apply(Stage &);
 
 private:
        virtual void visit(Block &);
@@ -72,9 +79,10 @@ private:
        virtual void visit(VariableDeclaration &);
        virtual void visit(Conditional &);
        virtual void visit(Iteration &);
-       using BlockModifier::visit;
 };
 
+/** Removes variable declarations with no references to them.  Assignment
+statements where the result is not used are also removed. */
 class UnusedVariableRemover: private TraversingVisitor
 {
 private:
@@ -97,7 +105,6 @@ private:
        Assignment *assignment;
        bool assignment_target;
        bool assign_to_subscript;
-       bool global_scope;
 
 public:
        UnusedVariableRemover();
@@ -106,6 +113,7 @@ public:
 
 private:
        virtual void visit(VariableReference &);
+       virtual void visit(InterfaceBlockReference &);
        virtual void visit(MemberAccess &);
        virtual void visit(BinaryExpression &);
        virtual void visit(Assignment &);
@@ -119,9 +127,9 @@ private:
        void merge_down_variables();
        virtual void visit(Conditional &);
        virtual void visit(Iteration &);
-       using TraversingVisitor::visit;
 };
 
+/** Removes function declarations with no references to them. */
 class UnusedFunctionRemover: private TraversingVisitor
 {
 private:
@@ -134,7 +142,6 @@ public:
 private:
        virtual void visit(FunctionCall &);
        virtual void visit(FunctionDeclaration &);
-       using TraversingVisitor::visit;
 };
 
 } // namespace SL