namespace GL {
namespace SL {
+/** Assigns values to specialization constants, turning them into normal
+constants. */
+class ConstantSpecializer: private TraversingVisitor
+{
+private:
+ const std::map<std::string, int> *values;
+
+public:
+ ConstantSpecializer();
+
+ void apply(Stage &, const std::map<std::string, int> &);
+
+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:
public:
InlineableFunctionLocator();
- const std::set<FunctionDeclaration *> &apply(Stage &s) { s.content.visit(*this); return inlineable; }
+ std::set<FunctionDeclaration *> apply(Stage &s) { s.content.visit(*this); return inlineable; }
private:
virtual void visit(FunctionCall &);
class InlineContentInjector: private TraversingVisitor
{
private:
+ enum Pass
+ {
+ REFERENCED,
+ INLINE,
+ RENAME
+ };
+
FunctionDeclaration *source_func;
- Block *target_block;
- std::map<std::string, VariableDeclaration *> variable_map;
- std::string remap_prefix;
- unsigned remap_names;
+ Block staging_block;
+ Pass pass;
RefPtr<Statement> r_inlined_statement;
std::set<Node *> dependencies;
std::set<std::string> referenced_names;
public:
InlineContentInjector();
- const std::string &apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionDeclaration &);
+ std::string apply(Stage &, FunctionDeclaration &, Block &, const NodeList<Statement>::iterator &, FunctionCall &);
private:
virtual void visit(VariableReference &);
class ExpressionInliner: private TraversingVisitor
{
private:
+ struct ExpressionUse
+ {
+ RefPtr<Expression> *reference;
+ Block *ref_scope;
+ bool blocked;
+
+ ExpressionUse(): reference(0), ref_scope(0), blocked(false) { }
+ };
+
struct ExpressionInfo
{
- Expression *expression;
+ Assignment::Target target;
+ RefPtr<Expression> expression;
Block *assign_scope;
- RefPtr<Expression> *inline_point;
+ std::vector<ExpressionUse> uses;
bool trivial;
- bool available;
- ExpressionInfo();
+ ExpressionInfo(): expression(0), assign_scope(0), trivial(false) { }
};
- std::map<Assignment::Target, ExpressionInfo> expressions;
+ std::list<ExpressionInfo> expressions;
+ std::map<Assignment::Target, ExpressionInfo *> assignments;
ExpressionInfo *r_ref_info;
- bool r_any_inlined;
bool r_trivial;
- bool mutating;
+ bool access_read;
+ bool access_write;
bool iteration_init;
Block *iteration_body;
const Operator *r_oper;
bool apply(Stage &);
private:
- void inline_expression(Expression &, RefPtr<Expression> &);
- virtual void visit(Block &);
virtual void visit(RefPtr<Expression> &);
virtual void visit(VariableReference &);
virtual void visit(MemberAccess &);
bool apply(Stage &s) { s.content.visit(*this); return r_any_folded; }
private:
- static BasicTypeDeclaration::Kind get_value_kind(const Variant &);
template<typename T>
static T evaluate_logical(char, T, T);
template<typename T>
static bool evaluate_relation(const char *, T, T);
template<typename T>
static T evaluate_arithmetic(char, T, T);
+ template<typename T>
+ static T evaluate_int_special_op(char, T, T);
+ template<typename T>
+ void convert_to_result(const Variant &);
void set_result(const Variant &, bool = false);
virtual void visit(RefPtr<Expression> &);
virtual void visit(Iteration &);
};
+class UnreachableCodeRemover: private TraversingVisitor
+{
+private:
+ bool reachable;
+ std::set<Node *> 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
{
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<Expression> &);
virtual void visit(BasicTypeDeclaration &);
virtual void visit(ImageTypeDeclaration &);
virtual void visit(StructDeclaration &);
Node *node;
Assignment::Target target;
std::vector<Node *> used_by;
+ unsigned in_loop;
AssignmentInfo(): node(0) { }
};
Assignment *r_assignment;
bool assignment_target;
bool r_side_effects;
+ bool in_struct;
+ bool composite_reference;
+ unsigned in_loop;
+ std::vector<Node *> loop_ext_refs;
+ Assignment::Target r_reference;
std::set<Node *> unused_nodes;
public:
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 &);