Support self-assignment elimination in the constant-folder.
Interestingly, this improves our codegen even with the optimizer fully
enabled, as apparently statement chains like:
`x = true; x = x; x = x;`
were getting transformed by constant-propagation into:
`x = true; x = true; x = true;`
making them no longer candidates for self-assignment elimination.
Change-Id: I6d94a809e94b01a00fd92459fcbce898b3cbbb11
Bug: skia:11343
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/377100
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
diff --git a/src/sksl/SkSLConstantFolder.cpp b/src/sksl/SkSLConstantFolder.cpp
index fc86d90..355b53c 100644
--- a/src/sksl/SkSLConstantFolder.cpp
+++ b/src/sksl/SkSLConstantFolder.cpp
@@ -195,6 +195,13 @@
return right.clone();
}
+ // If this is the assignment operator, and both sides are the same trivial expression, this is
+ // self-assignment (i.e., `var = var`) and can be reduced to just a variable reference (`var`).
+ // This can happen when other parts of the assignment are optimized away.
+ if (op.kind() == Token::Kind::TK_EQ && Analysis::IsSelfAssignment(left, right)) {
+ return right.clone();
+ }
+
// Simplify the expression when both sides are constant Boolean literals.
if (left.is<BoolLiteral>() && right.is<BoolLiteral>()) {
bool leftVal = left.as<BoolLiteral>().value();