diff options
author | Ethan Nicholas <ethannicholas@google.com> | 2017-04-20 19:31:52 -0400 |
---|---|---|
committer | Skia Commit-Bot <skia-commit-bot@chromium.org> | 2017-04-20 23:57:21 +0000 |
commit | cb67096b61f699b047fe8635984db1ac708a7b99 (patch) | |
tree | 3a8bda0962ad77d0d9ccb1d89c15f70578a7fe78 /src/sksl/SkSLCFGGenerator.h | |
parent | e79b473714866682dea85b66c005c5bb2ff6a397 (diff) |
Improved skslc optimizer, particularly around vectors.
BUG=skia:
Change-Id: Idb364d9198f2ff84aad1eb68e236fb45ec1c86b7
Reviewed-on: https://skia-review.googlesource.com/8000
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
Reviewed-by: Ben Wagner <benjaminwagner@google.com>
Diffstat (limited to 'src/sksl/SkSLCFGGenerator.h')
-rw-r--r-- | src/sksl/SkSLCFGGenerator.h | 77 |
1 files changed, 74 insertions, 3 deletions
diff --git a/src/sksl/SkSLCFGGenerator.h b/src/sksl/SkSLCFGGenerator.h index 0a03d69fc6..885d9261ec 100644 --- a/src/sksl/SkSLCFGGenerator.h +++ b/src/sksl/SkSLCFGGenerator.h @@ -26,6 +26,42 @@ struct BasicBlock { kExpression_Kind }; + Node(Kind kind, bool constantPropagation, std::unique_ptr<Expression>* expression, + std::unique_ptr<Statement>* statement) + : fKind(kind) + , fConstantPropagation(constantPropagation) + , fExpression(expression) + , fStatement(statement) {} + + std::unique_ptr<Expression>* expression() const { + ASSERT(fKind == kExpression_Kind); + return fExpression; + } + + void setExpression(std::unique_ptr<Expression> expr) { + ASSERT(fKind == kExpression_Kind); + *fExpression = std::move(expr); + } + + std::unique_ptr<Statement>* statement() const { + ASSERT(fKind == kStatement_Kind); + return fStatement; + } + + void setStatement(std::unique_ptr<Statement> stmt) { + ASSERT(fKind == kStatement_Kind); + *fStatement = std::move(stmt); + } + + String description() const { + if (fKind == kStatement_Kind) { + return (*fStatement)->description(); + } else { + ASSERT(fKind == kExpression_Kind); + return (*fExpression)->description(); + } + } + Kind fKind; // if false, this node should not be subject to constant propagation. This happens with // compound assignment (i.e. x *= 2), in which the value x is used as an rvalue for @@ -35,10 +71,45 @@ struct BasicBlock { // assignment if the target is constant (i.e. x = 1; x *= 2; should become x = 1; x = 1 * 2; // and then collapse down to a simple x = 2;). bool fConstantPropagation; + + private: + // we store pointers to the unique_ptrs so that we can replace expressions or statements + // during optimization without having to regenerate the entire tree std::unique_ptr<Expression>* fExpression; - const Statement* fStatement; + std::unique_ptr<Statement>* fStatement; }; + /** + * Attempts to remove the expression (and its subexpressions) pointed to by the iterator. If the + * expression can be cleanly removed, returns true and updates the iterator to point to the + * expression after the deleted expression. Otherwise returns false (and the CFG will need to be + * regenerated). + */ + bool tryRemoveExpression(std::vector<BasicBlock::Node>::iterator* iter); + + /** + * Locates and attempts remove an expression occurring before the expression pointed to by iter. + * If the expression can be cleanly removed, returns true and resets iter to a valid iterator + * pointing to the same expression it did initially. Otherwise returns false (and the CFG will + * need to be regenerated). + */ + bool tryRemoveExpressionBefore(std::vector<BasicBlock::Node>::iterator* iter, Expression* e); + + /** + * As tryRemoveExpressionBefore, but for lvalues. As lvalues are at most partially evaluated + * (for instance, x[i] = 0 evaluates i but not x) this will only look for the parts of the + * lvalue that are actually evaluated. + */ + bool tryRemoveLValueBefore(std::vector<BasicBlock::Node>::iterator* iter, Expression* lvalue); + + /** + * Attempts to inserts a new expression before the node pointed to by iter. If the + * expression can be cleanly inserted, returns true and updates the iterator to point to the + * newly inserted expression. Otherwise returns false (and the CFG will need to be regenerated). + */ + bool tryInsertExpression(std::vector<BasicBlock::Node>::iterator* iter, + std::unique_ptr<Expression>* expr); + std::vector<Node> fNodes; std::set<BlockId> fEntrances; std::set<BlockId> fExits; @@ -81,10 +152,10 @@ class CFGGenerator { public: CFGGenerator() {} - CFG getCFG(const FunctionDefinition& f); + CFG getCFG(FunctionDefinition& f); private: - void addStatement(CFG& cfg, const Statement* s); + void addStatement(CFG& cfg, std::unique_ptr<Statement>* s); void addExpression(CFG& cfg, std::unique_ptr<Expression>* e, bool constantPropagate); |