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