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