John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "src/sksl/SkSLConstantFolder.h" |
| 9 | |
| 10 | #include <limits> |
| 11 | |
| 12 | #include "src/sksl/SkSLContext.h" |
| 13 | #include "src/sksl/SkSLErrorReporter.h" |
| 14 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 15 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 16 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 17 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 18 | #include "src/sksl/ir/SkSLExpression.h" |
| 19 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 20 | #include "src/sksl/ir/SkSLIntLiteral.h" |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 21 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 22 | #include "src/sksl/ir/SkSLType.h" |
| 23 | #include "src/sksl/ir/SkSLVariable.h" |
| 24 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 25 | |
| 26 | namespace SkSL { |
| 27 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 28 | static std::unique_ptr<Expression> eliminate_no_op_boolean(const Expression& left, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 29 | Operator op, |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 30 | const Expression& right) { |
| 31 | SkASSERT(right.is<BoolLiteral>()); |
| 32 | bool rightVal = right.as<BoolLiteral>().value(); |
| 33 | |
| 34 | // Detect no-op Boolean expressions and optimize them away. |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 35 | if ((op.kind() == Token::Kind::TK_LOGICALAND && rightVal) || // (expr && true) -> (expr) |
| 36 | (op.kind() == Token::Kind::TK_LOGICALOR && !rightVal) || // (expr || false) -> (expr) |
| 37 | (op.kind() == Token::Kind::TK_LOGICALXOR && !rightVal) || // (expr ^^ false) -> (expr) |
| 38 | (op.kind() == Token::Kind::TK_EQEQ && rightVal) || // (expr == true) -> (expr) |
| 39 | (op.kind() == Token::Kind::TK_NEQ && !rightVal)) { // (expr != false) -> (expr) |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 40 | |
| 41 | return left.clone(); |
| 42 | } |
| 43 | |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 47 | static std::unique_ptr<Expression> short_circuit_boolean(const Expression& left, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 48 | Operator op, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 49 | const Expression& right) { |
| 50 | SkASSERT(left.is<BoolLiteral>()); |
| 51 | bool leftVal = left.as<BoolLiteral>().value(); |
| 52 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 53 | // When the literal is on the left, we can sometimes eliminate the other expression entirely. |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 54 | if ((op.kind() == Token::Kind::TK_LOGICALAND && !leftVal) || // (false && expr) -> (false) |
| 55 | (op.kind() == Token::Kind::TK_LOGICALOR && leftVal)) { // (true || expr) -> (true) |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 56 | |
| 57 | return left.clone(); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 58 | } |
| 59 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 60 | // We can't eliminate the right-side expression via short-circuit, but we might still be able to |
| 61 | // simplify away a no-op expression. |
| 62 | return eliminate_no_op_boolean(right, op, left); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 65 | // 'T' is the actual stored type of the literal data (SKSL_FLOAT or SKSL_INT). |
| 66 | // 'U' is an unsigned version of that, used to perform addition, subtraction, and multiplication, |
| 67 | // to avoid signed-integer overflow errors. This mimics the use of URESULT vs. RESULT when doing |
| 68 | // scalar folding in Simplify, later in this file. |
| 69 | template <typename T, typename U = T> |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 70 | static std::unique_ptr<Expression> simplify_vector(const Context& context, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 71 | const Expression& left, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 72 | Operator op, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 73 | const Expression& right) { |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 74 | SkASSERT(left.type().isVector()); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 75 | SkASSERT(left.type() == right.type()); |
| 76 | const Type& type = left.type(); |
| 77 | |
| 78 | // Handle boolean operations: == != |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 79 | if (op.kind() == Token::Kind::TK_EQEQ || op.kind() == Token::Kind::TK_NEQ) { |
| 80 | bool equality = (op.kind() == Token::Kind::TK_EQEQ); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 81 | |
| 82 | switch (left.compareConstant(right)) { |
| 83 | case Expression::ComparisonResult::kNotEqual: |
| 84 | equality = !equality; |
| 85 | [[fallthrough]]; |
| 86 | |
| 87 | case Expression::ComparisonResult::kEqual: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 88 | return BoolLiteral::Make(context, left.fOffset, equality); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 89 | |
| 90 | case Expression::ComparisonResult::kUnknown: |
| 91 | return nullptr; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Handle floating-point arithmetic: + - * / |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 96 | const auto vectorComponentwiseFold = [&](auto foldFn) -> std::unique_ptr<Expression> { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 97 | const Type& componentType = type.componentType(); |
| 98 | ExpressionArray args; |
| 99 | args.reserve_back(type.columns()); |
| 100 | for (int i = 0; i < type.columns(); i++) { |
John Stiles | 21a50ec | 2021-04-06 14:49:36 -0400 | [diff] [blame] | 101 | U value = foldFn(left.getConstantSubexpression(i)->as<Literal<T>>().value(), |
| 102 | right.getConstantSubexpression(i)->as<Literal<T>>().value()); |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 103 | args.push_back(Literal<T>::Make(left.fOffset, value, &componentType)); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 104 | } |
John Stiles | 23521a8 | 2021-03-02 17:02:51 -0500 | [diff] [blame] | 105 | auto foldedCtor = Constructor::Convert(context, left.fOffset, type, std::move(args)); |
| 106 | SkASSERT(foldedCtor); |
| 107 | return foldedCtor; |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 108 | }; |
| 109 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 110 | switch (op.kind()) { |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 111 | case Token::Kind::TK_PLUS: return vectorComponentwiseFold([](U a, U b) { return a + b; }); |
| 112 | case Token::Kind::TK_MINUS: return vectorComponentwiseFold([](U a, U b) { return a - b; }); |
| 113 | case Token::Kind::TK_STAR: return vectorComponentwiseFold([](U a, U b) { return a * b; }); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 114 | case Token::Kind::TK_SLASH: return vectorComponentwiseFold([](T a, T b) { return a / b; }); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 115 | default: |
| 116 | return nullptr; |
| 117 | } |
| 118 | } |
| 119 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 120 | static std::unique_ptr<Expression> cast_expression(const Context& context, |
| 121 | const Expression& expr, |
| 122 | const Type& type) { |
| 123 | ExpressionArray ctorArgs; |
| 124 | ctorArgs.push_back(expr.clone()); |
| 125 | std::unique_ptr<Expression> ctor = Constructor::Convert(context, expr.fOffset, type, |
| 126 | std::move(ctorArgs)); |
| 127 | SkASSERT(ctor); |
| 128 | return ctor; |
| 129 | } |
| 130 | |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 131 | static ConstructorSplat splat_scalar(const Expression& scalar, const Type& type) { |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 132 | SkASSERT(type.isVector()); |
| 133 | SkASSERT(type.componentType() == scalar.type()); |
| 134 | |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 135 | // Use a constructor to splat the scalar expression across a vector. |
| 136 | return ConstructorSplat{scalar.fOffset, type, scalar.clone()}; |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 139 | bool ConstantFolder::GetConstantInt(const Expression& value, SKSL_INT* out) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 140 | const Expression* expr = GetConstantValueForVariable(value); |
| 141 | if (!expr->is<IntLiteral>()) { |
| 142 | return false; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 143 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 144 | *out = expr->as<IntLiteral>().value(); |
| 145 | return true; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | bool ConstantFolder::GetConstantFloat(const Expression& value, SKSL_FLOAT* out) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 149 | const Expression* expr = GetConstantValueForVariable(value); |
| 150 | if (!expr->is<FloatLiteral>()) { |
| 151 | return false; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 152 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 153 | *out = expr->as<FloatLiteral>().value(); |
| 154 | return true; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 155 | } |
| 156 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 157 | static bool is_constant_scalar_value(const Expression& inExpr, float match) { |
| 158 | const Expression* expr = ConstantFolder::GetConstantValueForVariable(inExpr); |
| 159 | return (expr->is<IntLiteral>() && expr->as<IntLiteral>().value() == match) || |
| 160 | (expr->is<FloatLiteral>() && expr->as<FloatLiteral>().value() == match); |
| 161 | } |
| 162 | |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 163 | static bool contains_constant_zero(const Expression& expr) { |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 164 | if (expr.isAnyConstructor()) { |
| 165 | for (const auto& arg : expr.asAnyConstructor().argumentSpan()) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 166 | if (contains_constant_zero(*arg)) { |
| 167 | return true; |
| 168 | } |
| 169 | } |
| 170 | return false; |
| 171 | } |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 172 | return is_constant_scalar_value(expr, 0.0); |
| 173 | } |
| 174 | |
| 175 | static bool is_constant_value(const Expression& expr, float value) { |
| 176 | // This check only supports scalars and vectors (and in particular, not matrices). |
| 177 | SkASSERT(expr.type().isScalar() || expr.type().isVector()); |
| 178 | |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 179 | if (expr.isAnyConstructor()) { |
| 180 | for (const auto& arg : expr.asAnyConstructor().argumentSpan()) { |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 181 | if (!is_constant_value(*arg, value)) { |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | return is_constant_scalar_value(expr, value); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 188 | } |
| 189 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 190 | bool ConstantFolder::ErrorOnDivideByZero(const Context& context, int offset, Operator op, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 191 | const Expression& right) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 192 | switch (op.kind()) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 193 | case Token::Kind::TK_SLASH: |
| 194 | case Token::Kind::TK_SLASHEQ: |
| 195 | case Token::Kind::TK_PERCENT: |
| 196 | case Token::Kind::TK_PERCENTEQ: |
| 197 | if (contains_constant_zero(right)) { |
| 198 | context.fErrors.error(offset, "division by zero"); |
| 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | default: |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 207 | const Expression* ConstantFolder::GetConstantValueForVariable(const Expression& inExpr) { |
| 208 | for (const Expression* expr = &inExpr;;) { |
| 209 | if (!expr->is<VariableReference>()) { |
| 210 | break; |
| 211 | } |
| 212 | const VariableReference& varRef = expr->as<VariableReference>(); |
| 213 | if (varRef.refKind() != VariableRefKind::kRead) { |
| 214 | break; |
| 215 | } |
| 216 | const Variable& var = *varRef.variable(); |
| 217 | if (!(var.modifiers().fFlags & Modifiers::kConst_Flag)) { |
| 218 | break; |
| 219 | } |
| 220 | expr = var.initialValue(); |
John Stiles | ca65f8f | 2021-03-29 17:14:39 -0400 | [diff] [blame] | 221 | if (!expr) { |
| 222 | SkDEBUGFAILF("found a const variable without an initial value (%s)", |
| 223 | var.description().c_str()); |
| 224 | break; |
| 225 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 226 | if (expr->isCompileTimeConstant()) { |
| 227 | return expr; |
| 228 | } |
| 229 | if (!expr->is<VariableReference>()) { |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | // We didn't find a compile-time constant at the end. Return the expression as-is. |
| 234 | return &inExpr; |
| 235 | } |
| 236 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 237 | static std::unique_ptr<Expression> simplify_no_op_arithmetic(const Context& context, |
| 238 | const Expression& left, |
| 239 | Operator op, |
| 240 | const Expression& right, |
| 241 | const Type& resultType) { |
| 242 | switch (op.kind()) { |
| 243 | case Token::Kind::TK_PLUS: |
| 244 | if (is_constant_value(right, 0.0)) { // x + 0 |
| 245 | return cast_expression(context, left, resultType); |
| 246 | } |
| 247 | if (is_constant_value(left, 0.0)) { // 0 + x |
| 248 | return cast_expression(context, right, resultType); |
| 249 | } |
| 250 | break; |
| 251 | |
| 252 | case Token::Kind::TK_STAR: |
| 253 | if (is_constant_value(right, 1.0)) { // x * 1 |
| 254 | return cast_expression(context, left, resultType); |
| 255 | } |
| 256 | if (is_constant_value(left, 1.0)) { // 1 * x |
| 257 | return cast_expression(context, right, resultType); |
| 258 | } |
| 259 | if (is_constant_value(right, 0.0) && !left.hasSideEffects()) { // x * 0 |
| 260 | return cast_expression(context, right, resultType); |
| 261 | } |
| 262 | if (is_constant_value(left, 0.0) && !right.hasSideEffects()) { // 0 * x |
| 263 | return cast_expression(context, left, resultType); |
| 264 | } |
| 265 | break; |
| 266 | |
| 267 | case Token::Kind::TK_MINUS: |
| 268 | if (is_constant_value(right, 0.0)) { // x - 0 |
| 269 | return cast_expression(context, left, resultType); |
| 270 | } |
| 271 | if (is_constant_value(left, 0.0)) { // 0 - x (to `-x`) |
| 272 | return PrefixExpression::Make(context, Token::Kind::TK_MINUS, |
| 273 | cast_expression(context, right, resultType)); |
| 274 | } |
| 275 | break; |
| 276 | |
| 277 | case Token::Kind::TK_SLASH: |
| 278 | if (is_constant_value(right, 1.0)) { // x / 1 |
| 279 | return cast_expression(context, left, resultType); |
| 280 | } |
| 281 | if (is_constant_value(left, 0.0) && |
| 282 | !is_constant_value(right, 0.0) && |
| 283 | !right.hasSideEffects()) { // 0 / x (where x is not 0) |
| 284 | return cast_expression(context, left, resultType); |
| 285 | } |
| 286 | break; |
| 287 | |
| 288 | case Token::Kind::TK_PLUSEQ: |
| 289 | case Token::Kind::TK_MINUSEQ: |
| 290 | if (is_constant_value(right, 0.0)) { // x += 0, x -= 0 |
| 291 | std::unique_ptr<Expression> result = cast_expression(context, left, resultType); |
| 292 | Analysis::UpdateRefKind(result.get(), VariableRefKind::kRead); |
| 293 | return result; |
| 294 | } |
| 295 | break; |
| 296 | |
| 297 | case Token::Kind::TK_STAREQ: |
| 298 | case Token::Kind::TK_SLASHEQ: |
| 299 | if (is_constant_value(right, 1.0)) { // x *= 1, x /= 1 |
| 300 | std::unique_ptr<Expression> result = cast_expression(context, left, resultType); |
| 301 | Analysis::UpdateRefKind(result.get(), VariableRefKind::kRead); |
| 302 | return result; |
| 303 | } |
| 304 | break; |
| 305 | |
| 306 | default: |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | return nullptr; |
| 311 | } |
| 312 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 313 | std::unique_ptr<Expression> ConstantFolder::Simplify(const Context& context, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 314 | int offset, |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 315 | const Expression& leftExpr, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 316 | Operator op, |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 317 | const Expression& rightExpr, |
| 318 | const Type& resultType) { |
John Stiles | be6cbf3 | 2021-03-09 20:15:17 -0500 | [diff] [blame] | 319 | // When optimization is enabled, replace constant variables with trivial initial-values. |
| 320 | const Expression* left; |
| 321 | const Expression* right; |
| 322 | if (context.fConfig->fSettings.fOptimize) { |
| 323 | left = GetConstantValueForVariable(leftExpr); |
| 324 | right = GetConstantValueForVariable(rightExpr); |
| 325 | } else { |
| 326 | left = &leftExpr; |
| 327 | right = &rightExpr; |
| 328 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 329 | |
John Stiles | abc3b78 | 2021-02-05 10:07:19 -0500 | [diff] [blame] | 330 | // If this is the comma operator, the left side is evaluated but not otherwise used in any way. |
| 331 | // So if the left side has no side effects, it can just be eliminated entirely. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 332 | if (op.kind() == Token::Kind::TK_COMMA && !left->hasSideEffects()) { |
| 333 | return right->clone(); |
John Stiles | abc3b78 | 2021-02-05 10:07:19 -0500 | [diff] [blame] | 334 | } |
| 335 | |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 336 | // If this is the assignment operator, and both sides are the same trivial expression, this is |
| 337 | // self-assignment (i.e., `var = var`) and can be reduced to just a variable reference (`var`). |
| 338 | // This can happen when other parts of the assignment are optimized away. |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 339 | if (op.kind() == Token::Kind::TK_EQ && Analysis::IsSameExpressionTree(*left, *right)) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 340 | return right->clone(); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 341 | } |
| 342 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 343 | // Simplify the expression when both sides are constant Boolean literals. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 344 | if (left->is<BoolLiteral>() && right->is<BoolLiteral>()) { |
| 345 | bool leftVal = left->as<BoolLiteral>().value(); |
| 346 | bool rightVal = right->as<BoolLiteral>().value(); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 347 | bool result; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 348 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 349 | case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break; |
| 350 | case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break; |
| 351 | case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break; |
John Stiles | 26fdcbb | 2021-01-19 19:00:31 -0500 | [diff] [blame] | 352 | case Token::Kind::TK_EQEQ: result = leftVal == rightVal; break; |
| 353 | case Token::Kind::TK_NEQ: result = leftVal != rightVal; break; |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 354 | default: return nullptr; |
| 355 | } |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 356 | return BoolLiteral::Make(context, offset, result); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 357 | } |
| 358 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 359 | // If the left side is a Boolean literal, apply short-circuit optimizations. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 360 | if (left->is<BoolLiteral>()) { |
| 361 | return short_circuit_boolean(*left, op, *right); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // If the right side is a Boolean literal... |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 365 | if (right->is<BoolLiteral>()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 366 | // ... and the left side has no side effects... |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 367 | if (!left->hasSideEffects()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 368 | // We can reverse the expressions and short-circuit optimizations are still valid. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 369 | return short_circuit_boolean(*right, op, *left); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // We can't use short-circuiting, but we can still optimize away no-op Boolean expressions. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 373 | return eliminate_no_op_boolean(*left, op, *right); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 374 | } |
| 375 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 376 | if (op.kind() == Token::Kind::TK_EQEQ && Analysis::IsSameExpressionTree(*left, *right)) { |
| 377 | // With == comparison, if both sides are the same trivial expression, this is self- |
| 378 | // comparison and is always true. (We are not concerned with NaN.) |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 379 | return BoolLiteral::Make(context, leftExpr.fOffset, /*value=*/true); |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (op.kind() == Token::Kind::TK_NEQ && Analysis::IsSameExpressionTree(*left, *right)) { |
| 383 | // With != comparison, if both sides are the same trivial expression, this is self- |
| 384 | // comparison and is always false. (We are not concerned with NaN.) |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 385 | return BoolLiteral::Make(context, leftExpr.fOffset, /*value=*/false); |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 386 | } |
| 387 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 388 | if (ErrorOnDivideByZero(context, offset, op, *right)) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 389 | return nullptr; |
| 390 | } |
| 391 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 392 | // Optimize away no-op arithmetic like `x * 1`, `x *= 1`, `x + 0`, `x * 0`, `0 / x`, etc. |
| 393 | const Type& leftType = left->type(); |
| 394 | const Type& rightType = right->type(); |
| 395 | if ((leftType.isScalar() || leftType.isVector()) && |
| 396 | (rightType.isScalar() || rightType.isVector())) { |
| 397 | std::unique_ptr<Expression> expr = simplify_no_op_arithmetic(context, *left, op, *right, |
| 398 | resultType); |
| 399 | if (expr) { |
| 400 | return expr; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Other than the cases above, constant folding requires both sides to be constant. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 405 | if (!left->isCompileTimeConstant() || !right->isCompileTimeConstant()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 406 | return nullptr; |
| 407 | } |
| 408 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 409 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 410 | // precision to calculate the results and hope the result makes sense. |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 411 | // TODO(skia:10932): detect and handle integer overflow properly. |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 412 | using SKSL_UINT = uint64_t; |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 413 | #define RESULT(t, op) t ## Literal::Make(context, offset, leftVal op rightVal) |
| 414 | #define URESULT(t, op) t ## Literal::Make(context, offset, (SKSL_UINT) leftVal op \ |
| 415 | (SKSL_UINT) rightVal) |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 416 | if (left->is<IntLiteral>() && right->is<IntLiteral>()) { |
| 417 | SKSL_INT leftVal = left->as<IntLiteral>().value(); |
| 418 | SKSL_INT rightVal = right->as<IntLiteral>().value(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 419 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 420 | case Token::Kind::TK_PLUS: return URESULT(Int, +); |
| 421 | case Token::Kind::TK_MINUS: return URESULT(Int, -); |
| 422 | case Token::Kind::TK_STAR: return URESULT(Int, *); |
| 423 | case Token::Kind::TK_SLASH: |
| 424 | if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 425 | context.fErrors.error(offset, "arithmetic overflow"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 426 | return nullptr; |
| 427 | } |
| 428 | return RESULT(Int, /); |
| 429 | case Token::Kind::TK_PERCENT: |
| 430 | if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 431 | context.fErrors.error(offset, "arithmetic overflow"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 432 | return nullptr; |
| 433 | } |
| 434 | return RESULT(Int, %); |
| 435 | case Token::Kind::TK_BITWISEAND: return RESULT(Int, &); |
| 436 | case Token::Kind::TK_BITWISEOR: return RESULT(Int, |); |
| 437 | case Token::Kind::TK_BITWISEXOR: return RESULT(Int, ^); |
| 438 | case Token::Kind::TK_EQEQ: return RESULT(Bool, ==); |
| 439 | case Token::Kind::TK_NEQ: return RESULT(Bool, !=); |
| 440 | case Token::Kind::TK_GT: return RESULT(Bool, >); |
| 441 | case Token::Kind::TK_GTEQ: return RESULT(Bool, >=); |
| 442 | case Token::Kind::TK_LT: return RESULT(Bool, <); |
| 443 | case Token::Kind::TK_LTEQ: return RESULT(Bool, <=); |
| 444 | case Token::Kind::TK_SHL: |
| 445 | if (rightVal >= 0 && rightVal <= 31) { |
Brian Osman | a7eb681 | 2021-02-01 11:43:05 -0500 | [diff] [blame] | 446 | // Left-shifting a negative (or really, any signed) value is undefined behavior |
| 447 | // in C++, but not GLSL. Do the shift on unsigned values, to avoid UBSAN. |
| 448 | return URESULT(Int, <<); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 449 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 450 | context.fErrors.error(offset, "shift value out of range"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 451 | return nullptr; |
| 452 | case Token::Kind::TK_SHR: |
| 453 | if (rightVal >= 0 && rightVal <= 31) { |
| 454 | return RESULT(Int, >>); |
| 455 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 456 | context.fErrors.error(offset, "shift value out of range"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 457 | return nullptr; |
| 458 | |
| 459 | default: |
| 460 | return nullptr; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Perform constant folding on pairs of floating-point literals. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 465 | if (left->is<FloatLiteral>() && right->is<FloatLiteral>()) { |
| 466 | SKSL_FLOAT leftVal = left->as<FloatLiteral>().value(); |
| 467 | SKSL_FLOAT rightVal = right->as<FloatLiteral>().value(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 468 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 469 | case Token::Kind::TK_PLUS: return RESULT(Float, +); |
| 470 | case Token::Kind::TK_MINUS: return RESULT(Float, -); |
| 471 | case Token::Kind::TK_STAR: return RESULT(Float, *); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 472 | case Token::Kind::TK_SLASH: return RESULT(Float, /); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 473 | case Token::Kind::TK_EQEQ: return RESULT(Bool, ==); |
| 474 | case Token::Kind::TK_NEQ: return RESULT(Bool, !=); |
| 475 | case Token::Kind::TK_GT: return RESULT(Bool, >); |
| 476 | case Token::Kind::TK_GTEQ: return RESULT(Bool, >=); |
| 477 | case Token::Kind::TK_LT: return RESULT(Bool, <); |
| 478 | case Token::Kind::TK_LTEQ: return RESULT(Bool, <=); |
| 479 | default: return nullptr; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // Perform constant folding on pairs of vectors. |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 484 | if (leftType.isVector() && leftType == rightType) { |
| 485 | if (leftType.componentType().isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 486 | return simplify_vector<SKSL_FLOAT>(context, *left, op, *right); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 487 | } |
| 488 | if (leftType.componentType().isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 489 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, *left, op, *right); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 490 | } |
| 491 | return nullptr; |
| 492 | } |
| 493 | |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 494 | // Perform constant folding on vectors against scalars, e.g.: half4(2) + 2 |
| 495 | if (leftType.isVector() && leftType.componentType() == rightType) { |
| 496 | if (rightType.isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 497 | return simplify_vector<SKSL_FLOAT>(context, *left, op, |
| 498 | splat_scalar(*right, left->type())); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 499 | } |
| 500 | if (rightType.isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 501 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, *left, op, |
| 502 | splat_scalar(*right, left->type())); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 503 | } |
| 504 | return nullptr; |
| 505 | } |
| 506 | |
| 507 | // Perform constant folding on scalars against vectors, e.g.: 2 + half4(2) |
| 508 | if (rightType.isVector() && rightType.componentType() == leftType) { |
| 509 | if (leftType.isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 510 | return simplify_vector<SKSL_FLOAT>(context, splat_scalar(*left, right->type()), op, |
| 511 | *right); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 512 | } |
| 513 | if (leftType.isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 514 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, splat_scalar(*left, right->type()), |
| 515 | op, *right); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 516 | } |
| 517 | return nullptr; |
| 518 | } |
| 519 | |
John Stiles | 22a5454 | 2021-03-31 11:33:48 -0400 | [diff] [blame] | 520 | // Perform constant folding on pairs of matrices or arrays. |
| 521 | if ((leftType.isMatrix() && rightType.isMatrix()) || |
| 522 | (leftType.isArray() && rightType.isArray())) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 523 | bool equality; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 524 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 525 | case Token::Kind::TK_EQEQ: |
| 526 | equality = true; |
| 527 | break; |
| 528 | case Token::Kind::TK_NEQ: |
| 529 | equality = false; |
| 530 | break; |
| 531 | default: |
| 532 | return nullptr; |
| 533 | } |
| 534 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 535 | switch (left->compareConstant(*right)) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 536 | case Expression::ComparisonResult::kNotEqual: |
| 537 | equality = !equality; |
| 538 | [[fallthrough]]; |
| 539 | |
| 540 | case Expression::ComparisonResult::kEqual: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 541 | return BoolLiteral::Make(context, offset, equality); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 542 | |
| 543 | case Expression::ComparisonResult::kUnknown: |
| 544 | return nullptr; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // We aren't able to constant-fold. |
| 549 | #undef RESULT |
| 550 | #undef URESULT |
| 551 | return nullptr; |
| 552 | } |
| 553 | |
| 554 | } // namespace SkSL |