X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Foptimize.h;h=7d064056e48ceb6d72bb7d9a4b2e9d5099f2e401;hb=4737d137d0a1c7fed868c4adc7a3d7e00ba7681c;hp=160388065b8fab22d492e3193f8eed9702f76acb;hpb=c4ae3a80623be4b0f17de4a9cc25f0bedf2dbef5;p=libs%2Fgl.git diff --git a/source/glsl/optimize.h b/source/glsl/optimize.h index 16038806..7d064056 100644 --- a/source/glsl/optimize.h +++ b/source/glsl/optimize.h @@ -9,9 +9,25 @@ namespace Msp { namespace GL { namespace SL { +/** Assigns values to specialization constants, turning them into normal +constants. */ +class ConstantSpecializer: private TraversingVisitor +{ +private: + const std::map *values; + +public: + ConstantSpecializer(); + + void apply(Stage &, const std::map &); + +private: + virtual void visit(VariableDeclaration &); +}; + /** Finds functions which are candidates for inlining. Currently this means -functions which have no parameters, contain no more than one return statement, -and are only called once. */ +functions which have no flow control statements, no more than one return +statement, and are either builtins or only called once. */ class InlineableFunctionLocator: private TraversingVisitor { private: @@ -23,7 +39,7 @@ private: public: InlineableFunctionLocator(); - const std::set &apply(Stage &s) { s.content.visit(*this); return inlineable; } + std::set apply(Stage &s) { s.content.visit(*this); return inlineable; } private: virtual void visit(FunctionCall &); @@ -39,12 +55,16 @@ dependencies of the inlined statements to appear before the target function. */ class InlineContentInjector: private TraversingVisitor { private: + enum Pass + { + REFERENCED, + INLINE, + RENAME + }; + FunctionDeclaration *source_func; - Block *target_block; - std::map variable_map; - std::string remap_prefix; - unsigned remap_names; - bool deps_only; + Block staging_block; + Pass pass; RefPtr r_inlined_statement; std::set dependencies; std::set referenced_names; @@ -53,7 +73,7 @@ private: public: InlineContentInjector(); - const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionDeclaration &); + std::string apply(Stage &, FunctionDeclaration &, Block &, const NodeList::iterator &, FunctionCall &); private: virtual void visit(VariableReference &); @@ -75,6 +95,7 @@ private: NodeList::iterator insert_point; RefPtr r_inline_result; bool r_any_inlined; + bool r_inlined_here; public: FunctionInliner(); @@ -103,7 +124,7 @@ private: bool trivial; bool available; - ExpressionInfo(); + ExpressionInfo(): expression(0), assign_scope(0), inline_point(0), trivial(false), available(true) { } }; std::map expressions; @@ -154,13 +175,16 @@ public: bool apply(Stage &s) { s.content.visit(*this); return r_any_folded; } private: - static BasicTypeDeclaration::Kind get_value_kind(const Variant &); template static T evaluate_logical(char, T, T); template static bool evaluate_relation(const char *, T, T); template static T evaluate_arithmetic(char, T, T); + template + static T evaluate_int_special_op(char, T, T); + template + void convert_to_result(const Variant &); void set_result(const Variant &, bool = false); virtual void visit(RefPtr &); @@ -206,6 +230,26 @@ private: virtual void visit(Iteration &); }; +class UnreachableCodeRemover: private TraversingVisitor +{ +private: + bool reachable; + std::set unreachable_nodes; + +public: + UnreachableCodeRemover(); + + virtual bool apply(Stage &); + +private: + virtual void visit(Block &); + virtual void visit(FunctionDeclaration &); + virtual void visit(Conditional &); + virtual void visit(Iteration &); + virtual void visit(Return &) { reachable = false; } + virtual void visit(Jump &) { reachable = false; } +}; + /** Removes types which are not used anywhere. */ class UnusedTypeRemover: private TraversingVisitor { @@ -216,11 +260,7 @@ public: bool apply(Stage &); private: - virtual void visit(Literal &); - virtual void visit(UnaryExpression &); - virtual void visit(BinaryExpression &); - virtual void visit(TernaryExpression &); - virtual void visit(FunctionCall &); + virtual void visit(RefPtr &); virtual void visit(BasicTypeDeclaration &); virtual void visit(ImageTypeDeclaration &); virtual void visit(StructDeclaration &); @@ -263,6 +303,9 @@ private: Assignment *r_assignment; bool assignment_target; bool r_side_effects; + bool in_struct; + bool composite_reference; + Assignment::Target r_reference; std::set unused_nodes; public: @@ -274,14 +317,17 @@ private: void referenced(const Assignment::Target &, Node &); virtual void visit(VariableReference &); virtual void visit(InterfaceBlockReference &); + void visit_composite(Expression &); + virtual void visit(MemberAccess &); + virtual void visit(Swizzle &); virtual void visit(UnaryExpression &); virtual void visit(BinaryExpression &); virtual void visit(Assignment &); + virtual void visit(TernaryExpression &); virtual void visit(FunctionCall &); void record_assignment(const Assignment::Target &, Node &); virtual void visit(ExpressionStatement &); - // Ignore structs because their members can't be accessed directly. - virtual void visit(StructDeclaration &) { } + virtual void visit(StructDeclaration &); virtual void visit(VariableDeclaration &); virtual void visit(InterfaceBlock &); void merge_variables(const BlockVariableMap &);