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 | 55dc5c8 | 2021-08-19 18:15:49 -0400 | [diff] [blame^] | 163 | int numSlots = expr.type().slotCount(); |
| 164 | for (int index = 0; index < numSlots; ++index) { |
| 165 | const Expression* subexpr = expr.getConstantSubexpression(index); |
| 166 | if (subexpr && is_constant_scalar_value(*subexpr, 0.0f)) { |
| 167 | return true; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 168 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 169 | } |
John Stiles | 55dc5c8 | 2021-08-19 18:15:49 -0400 | [diff] [blame^] | 170 | return false; |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static bool is_constant_value(const Expression& expr, float value) { |
John Stiles | 55dc5c8 | 2021-08-19 18:15:49 -0400 | [diff] [blame^] | 174 | int numSlots = expr.type().slotCount(); |
| 175 | for (int index = 0; index < numSlots; ++index) { |
| 176 | const Expression* subexpr = expr.getConstantSubexpression(index); |
| 177 | if (!subexpr || !is_constant_scalar_value(*subexpr, value)) { |
| 178 | return false; |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 179 | } |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 180 | } |
John Stiles | 55dc5c8 | 2021-08-19 18:15:49 -0400 | [diff] [blame^] | 181 | return true; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 182 | } |
| 183 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 184 | bool ConstantFolder::ErrorOnDivideByZero(const Context& context, int offset, Operator op, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 185 | const Expression& right) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 186 | switch (op.kind()) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 187 | case Token::Kind::TK_SLASH: |
| 188 | case Token::Kind::TK_SLASHEQ: |
| 189 | case Token::Kind::TK_PERCENT: |
| 190 | case Token::Kind::TK_PERCENTEQ: |
| 191 | if (contains_constant_zero(right)) { |
Ethan Nicholas | 8d11654 | 2021-08-11 13:27:16 -0400 | [diff] [blame] | 192 | context.errors().error(offset, "division by zero"); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | return false; |
| 196 | default: |
| 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 201 | const Expression* ConstantFolder::GetConstantValueForVariable(const Expression& inExpr) { |
| 202 | for (const Expression* expr = &inExpr;;) { |
| 203 | if (!expr->is<VariableReference>()) { |
| 204 | break; |
| 205 | } |
| 206 | const VariableReference& varRef = expr->as<VariableReference>(); |
| 207 | if (varRef.refKind() != VariableRefKind::kRead) { |
| 208 | break; |
| 209 | } |
| 210 | const Variable& var = *varRef.variable(); |
| 211 | if (!(var.modifiers().fFlags & Modifiers::kConst_Flag)) { |
| 212 | break; |
| 213 | } |
| 214 | expr = var.initialValue(); |
John Stiles | ca65f8f | 2021-03-29 17:14:39 -0400 | [diff] [blame] | 215 | if (!expr) { |
| 216 | SkDEBUGFAILF("found a const variable without an initial value (%s)", |
| 217 | var.description().c_str()); |
| 218 | break; |
| 219 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 220 | if (expr->isCompileTimeConstant()) { |
| 221 | return expr; |
| 222 | } |
| 223 | if (!expr->is<VariableReference>()) { |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | // We didn't find a compile-time constant at the end. Return the expression as-is. |
| 228 | return &inExpr; |
| 229 | } |
| 230 | |
John Stiles | ffba524 | 2021-05-07 10:50:22 -0400 | [diff] [blame] | 231 | std::unique_ptr<Expression> ConstantFolder::MakeConstantValueForVariable( |
| 232 | std::unique_ptr<Expression> expr) { |
| 233 | const Expression* constantExpr = GetConstantValueForVariable(*expr); |
| 234 | if (constantExpr != expr.get()) { |
| 235 | expr = constantExpr->clone(); |
| 236 | } |
| 237 | return expr; |
| 238 | } |
| 239 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 240 | static std::unique_ptr<Expression> simplify_no_op_arithmetic(const Context& context, |
| 241 | const Expression& left, |
| 242 | Operator op, |
| 243 | const Expression& right, |
| 244 | const Type& resultType) { |
| 245 | switch (op.kind()) { |
| 246 | case Token::Kind::TK_PLUS: |
| 247 | if (is_constant_value(right, 0.0)) { // x + 0 |
| 248 | return cast_expression(context, left, resultType); |
| 249 | } |
| 250 | if (is_constant_value(left, 0.0)) { // 0 + x |
| 251 | return cast_expression(context, right, resultType); |
| 252 | } |
| 253 | break; |
| 254 | |
| 255 | case Token::Kind::TK_STAR: |
| 256 | if (is_constant_value(right, 1.0)) { // x * 1 |
| 257 | return cast_expression(context, left, resultType); |
| 258 | } |
| 259 | if (is_constant_value(left, 1.0)) { // 1 * x |
| 260 | return cast_expression(context, right, resultType); |
| 261 | } |
| 262 | if (is_constant_value(right, 0.0) && !left.hasSideEffects()) { // x * 0 |
| 263 | return cast_expression(context, right, resultType); |
| 264 | } |
| 265 | if (is_constant_value(left, 0.0) && !right.hasSideEffects()) { // 0 * x |
| 266 | return cast_expression(context, left, resultType); |
| 267 | } |
| 268 | break; |
| 269 | |
| 270 | case Token::Kind::TK_MINUS: |
| 271 | if (is_constant_value(right, 0.0)) { // x - 0 |
| 272 | return cast_expression(context, left, resultType); |
| 273 | } |
| 274 | if (is_constant_value(left, 0.0)) { // 0 - x (to `-x`) |
| 275 | return PrefixExpression::Make(context, Token::Kind::TK_MINUS, |
| 276 | cast_expression(context, right, resultType)); |
| 277 | } |
| 278 | break; |
| 279 | |
| 280 | case Token::Kind::TK_SLASH: |
| 281 | if (is_constant_value(right, 1.0)) { // x / 1 |
| 282 | return cast_expression(context, left, resultType); |
| 283 | } |
| 284 | if (is_constant_value(left, 0.0) && |
| 285 | !is_constant_value(right, 0.0) && |
| 286 | !right.hasSideEffects()) { // 0 / x (where x is not 0) |
| 287 | return cast_expression(context, left, resultType); |
| 288 | } |
| 289 | break; |
| 290 | |
| 291 | case Token::Kind::TK_PLUSEQ: |
| 292 | case Token::Kind::TK_MINUSEQ: |
| 293 | if (is_constant_value(right, 0.0)) { // x += 0, x -= 0 |
| 294 | std::unique_ptr<Expression> result = cast_expression(context, left, resultType); |
| 295 | Analysis::UpdateRefKind(result.get(), VariableRefKind::kRead); |
| 296 | return result; |
| 297 | } |
| 298 | break; |
| 299 | |
| 300 | case Token::Kind::TK_STAREQ: |
| 301 | case Token::Kind::TK_SLASHEQ: |
| 302 | if (is_constant_value(right, 1.0)) { // x *= 1, x /= 1 |
| 303 | std::unique_ptr<Expression> result = cast_expression(context, left, resultType); |
| 304 | Analysis::UpdateRefKind(result.get(), VariableRefKind::kRead); |
| 305 | return result; |
| 306 | } |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | return nullptr; |
| 314 | } |
| 315 | |
John Stiles | afaddc9 | 2021-05-26 22:21:42 -0400 | [diff] [blame] | 316 | template <typename T> |
| 317 | static std::unique_ptr<Expression> fold_float_expression(int offset, |
| 318 | T result, |
| 319 | const Type* resultType) { |
| 320 | // 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] | 321 | if constexpr (!std::is_same<T, bool>::value) { |
John Stiles | afaddc9 | 2021-05-26 22:21:42 -0400 | [diff] [blame] | 322 | if (!std::isfinite(result)) { |
| 323 | return nullptr; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return Literal<T>::Make(offset, result, resultType); |
| 328 | } |
| 329 | |
John Stiles | 3598129 | 2021-05-27 12:26:37 -0400 | [diff] [blame] | 330 | template <typename T> |
| 331 | static std::unique_ptr<Expression> fold_int_expression(int offset, |
| 332 | T result, |
| 333 | const Type* resultType) { |
| 334 | // If constant-folding this expression would overflow the result type, leave it as-is. |
| 335 | if constexpr (!std::is_same<T, bool>::value) { |
| 336 | if (result < resultType->minimumValue() || result > resultType->maximumValue()) { |
| 337 | return nullptr; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return Literal<T>::Make(offset, result, resultType); |
| 342 | } |
| 343 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 344 | std::unique_ptr<Expression> ConstantFolder::Simplify(const Context& context, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 345 | int offset, |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 346 | const Expression& leftExpr, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 347 | Operator op, |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 348 | const Expression& rightExpr, |
| 349 | const Type& resultType) { |
John Stiles | be6cbf3 | 2021-03-09 20:15:17 -0500 | [diff] [blame] | 350 | // When optimization is enabled, replace constant variables with trivial initial-values. |
| 351 | const Expression* left; |
| 352 | const Expression* right; |
| 353 | if (context.fConfig->fSettings.fOptimize) { |
| 354 | left = GetConstantValueForVariable(leftExpr); |
| 355 | right = GetConstantValueForVariable(rightExpr); |
| 356 | } else { |
| 357 | left = &leftExpr; |
| 358 | right = &rightExpr; |
| 359 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 360 | |
John Stiles | abc3b78 | 2021-02-05 10:07:19 -0500 | [diff] [blame] | 361 | // If this is the comma operator, the left side is evaluated but not otherwise used in any way. |
| 362 | // 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] | 363 | if (op.kind() == Token::Kind::TK_COMMA && !left->hasSideEffects()) { |
| 364 | return right->clone(); |
John Stiles | abc3b78 | 2021-02-05 10:07:19 -0500 | [diff] [blame] | 365 | } |
| 366 | |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 367 | // If this is the assignment operator, and both sides are the same trivial expression, this is |
| 368 | // self-assignment (i.e., `var = var`) and can be reduced to just a variable reference (`var`). |
| 369 | // This can happen when other parts of the assignment are optimized away. |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 370 | if (op.kind() == Token::Kind::TK_EQ && Analysis::IsSameExpressionTree(*left, *right)) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 371 | return right->clone(); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 372 | } |
| 373 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 374 | // Simplify the expression when both sides are constant Boolean literals. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 375 | if (left->is<BoolLiteral>() && right->is<BoolLiteral>()) { |
| 376 | bool leftVal = left->as<BoolLiteral>().value(); |
| 377 | bool rightVal = right->as<BoolLiteral>().value(); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 378 | bool result; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 379 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 380 | case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break; |
| 381 | case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break; |
| 382 | case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break; |
John Stiles | 26fdcbb | 2021-01-19 19:00:31 -0500 | [diff] [blame] | 383 | case Token::Kind::TK_EQEQ: result = leftVal == rightVal; break; |
| 384 | case Token::Kind::TK_NEQ: result = leftVal != rightVal; break; |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 385 | default: return nullptr; |
| 386 | } |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 387 | return BoolLiteral::Make(context, offset, result); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 388 | } |
| 389 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 390 | // If the left side is a Boolean literal, apply short-circuit optimizations. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 391 | if (left->is<BoolLiteral>()) { |
| 392 | return short_circuit_boolean(*left, op, *right); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // If the right side is a Boolean literal... |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 396 | if (right->is<BoolLiteral>()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 397 | // ... and the left side has no side effects... |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 398 | if (!left->hasSideEffects()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 399 | // We can reverse the expressions and short-circuit optimizations are still valid. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 400 | return short_circuit_boolean(*right, op, *left); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // 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] | 404 | return eliminate_no_op_boolean(*left, op, *right); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 405 | } |
| 406 | |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 407 | if (op.kind() == Token::Kind::TK_EQEQ && Analysis::IsSameExpressionTree(*left, *right)) { |
| 408 | // With == comparison, if both sides are the same trivial expression, this is self- |
| 409 | // comparison and is always true. (We are not concerned with NaN.) |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 410 | return BoolLiteral::Make(context, leftExpr.fOffset, /*value=*/true); |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | if (op.kind() == Token::Kind::TK_NEQ && Analysis::IsSameExpressionTree(*left, *right)) { |
| 414 | // With != comparison, if both sides are the same trivial expression, this is self- |
| 415 | // comparison and is always false. (We are not concerned with NaN.) |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 416 | return BoolLiteral::Make(context, leftExpr.fOffset, /*value=*/false); |
John Stiles | 5676c57 | 2021-03-08 17:10:52 -0500 | [diff] [blame] | 417 | } |
| 418 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 419 | if (ErrorOnDivideByZero(context, offset, op, *right)) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 420 | return nullptr; |
| 421 | } |
| 422 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 423 | // Optimize away no-op arithmetic like `x * 1`, `x *= 1`, `x + 0`, `x * 0`, `0 / x`, etc. |
| 424 | const Type& leftType = left->type(); |
| 425 | const Type& rightType = right->type(); |
| 426 | if ((leftType.isScalar() || leftType.isVector()) && |
| 427 | (rightType.isScalar() || rightType.isVector())) { |
| 428 | std::unique_ptr<Expression> expr = simplify_no_op_arithmetic(context, *left, op, *right, |
| 429 | resultType); |
| 430 | if (expr) { |
| 431 | return expr; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | // 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] | 436 | if (!left->isCompileTimeConstant() || !right->isCompileTimeConstant()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 437 | return nullptr; |
| 438 | } |
| 439 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 440 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 441 | // precision to calculate the results and hope the result makes sense. |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 442 | // TODO(skia:10932): detect and handle integer overflow properly. |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 443 | using SKSL_UINT = uint64_t; |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 444 | if (left->is<IntLiteral>() && right->is<IntLiteral>()) { |
| 445 | SKSL_INT leftVal = left->as<IntLiteral>().value(); |
| 446 | SKSL_INT rightVal = right->as<IntLiteral>().value(); |
John Stiles | 3598129 | 2021-05-27 12:26:37 -0400 | [diff] [blame] | 447 | |
| 448 | #define RESULT(Op) fold_int_expression(offset, \ |
| 449 | (SKSL_INT)(leftVal) Op (SKSL_INT)(rightVal), &resultType) |
| 450 | #define URESULT(Op) fold_int_expression(offset, \ |
| 451 | (SKSL_INT)((SKSL_UINT)(leftVal) Op (SKSL_UINT)(rightVal)), &resultType) |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 452 | switch (op.kind()) { |
John Stiles | 3598129 | 2021-05-27 12:26:37 -0400 | [diff] [blame] | 453 | case Token::Kind::TK_PLUS: return URESULT(+); |
| 454 | case Token::Kind::TK_MINUS: return URESULT(-); |
| 455 | case Token::Kind::TK_STAR: return URESULT(*); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 456 | case Token::Kind::TK_SLASH: |
| 457 | if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) { |
Ethan Nicholas | 8d11654 | 2021-08-11 13:27:16 -0400 | [diff] [blame] | 458 | context.errors().error(offset, "arithmetic overflow"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 459 | return nullptr; |
| 460 | } |
John Stiles | 3598129 | 2021-05-27 12:26:37 -0400 | [diff] [blame] | 461 | return RESULT(/); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 462 | case Token::Kind::TK_PERCENT: |
| 463 | if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) { |
Ethan Nicholas | 8d11654 | 2021-08-11 13:27:16 -0400 | [diff] [blame] | 464 | context.errors().error(offset, "arithmetic overflow"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 465 | return nullptr; |
| 466 | } |
John Stiles | 3598129 | 2021-05-27 12:26:37 -0400 | [diff] [blame] | 467 | return RESULT(%); |
| 468 | case Token::Kind::TK_BITWISEAND: return RESULT(&); |
| 469 | case Token::Kind::TK_BITWISEOR: return RESULT(|); |
| 470 | case Token::Kind::TK_BITWISEXOR: return RESULT(^); |
| 471 | case Token::Kind::TK_EQEQ: return RESULT(==); |
| 472 | case Token::Kind::TK_NEQ: return RESULT(!=); |
| 473 | case Token::Kind::TK_GT: return RESULT(>); |
| 474 | case Token::Kind::TK_GTEQ: return RESULT(>=); |
| 475 | case Token::Kind::TK_LT: return RESULT(<); |
| 476 | case Token::Kind::TK_LTEQ: return RESULT(<=); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 477 | case Token::Kind::TK_SHL: |
| 478 | if (rightVal >= 0 && rightVal <= 31) { |
Brian Osman | a7eb681 | 2021-02-01 11:43:05 -0500 | [diff] [blame] | 479 | // Left-shifting a negative (or really, any signed) value is undefined behavior |
| 480 | // 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] | 481 | return URESULT(<<); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 482 | } |
Ethan Nicholas | 8d11654 | 2021-08-11 13:27:16 -0400 | [diff] [blame] | 483 | context.errors().error(offset, "shift value out of range"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 484 | return nullptr; |
| 485 | case Token::Kind::TK_SHR: |
| 486 | if (rightVal >= 0 && rightVal <= 31) { |
John Stiles | 3598129 | 2021-05-27 12:26:37 -0400 | [diff] [blame] | 487 | return RESULT(>>); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 488 | } |
Ethan Nicholas | 8d11654 | 2021-08-11 13:27:16 -0400 | [diff] [blame] | 489 | context.errors().error(offset, "shift value out of range"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 490 | return nullptr; |
| 491 | |
| 492 | default: |
| 493 | return nullptr; |
| 494 | } |
John Stiles | afaddc9 | 2021-05-26 22:21:42 -0400 | [diff] [blame] | 495 | #undef RESULT |
| 496 | #undef URESULT |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Perform constant folding on pairs of floating-point literals. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 500 | if (left->is<FloatLiteral>() && right->is<FloatLiteral>()) { |
| 501 | SKSL_FLOAT leftVal = left->as<FloatLiteral>().value(); |
| 502 | SKSL_FLOAT rightVal = right->as<FloatLiteral>().value(); |
John Stiles | afaddc9 | 2021-05-26 22:21:42 -0400 | [diff] [blame] | 503 | |
| 504 | #define RESULT(Op) fold_float_expression(offset, leftVal Op rightVal, &resultType) |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 505 | switch (op.kind()) { |
John Stiles | afaddc9 | 2021-05-26 22:21:42 -0400 | [diff] [blame] | 506 | case Token::Kind::TK_PLUS: return RESULT(+); |
| 507 | case Token::Kind::TK_MINUS: return RESULT(-); |
| 508 | case Token::Kind::TK_STAR: return RESULT(*); |
| 509 | case Token::Kind::TK_SLASH: return RESULT(/); |
| 510 | case Token::Kind::TK_EQEQ: return RESULT(==); |
| 511 | case Token::Kind::TK_NEQ: return RESULT(!=); |
| 512 | case Token::Kind::TK_GT: return RESULT(>); |
| 513 | case Token::Kind::TK_GTEQ: return RESULT(>=); |
| 514 | case Token::Kind::TK_LT: return RESULT(<); |
| 515 | case Token::Kind::TK_LTEQ: return RESULT(<=); |
| 516 | default: return nullptr; |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 517 | } |
John Stiles | afaddc9 | 2021-05-26 22:21:42 -0400 | [diff] [blame] | 518 | #undef RESULT |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | // Perform constant folding on pairs of vectors. |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 522 | if (leftType.isVector() && leftType == rightType) { |
| 523 | if (leftType.componentType().isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 524 | return simplify_vector<SKSL_FLOAT>(context, *left, op, *right); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 525 | } |
| 526 | if (leftType.componentType().isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 527 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, *left, op, *right); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 528 | } |
| 529 | return nullptr; |
| 530 | } |
| 531 | |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 532 | // Perform constant folding on vectors against scalars, e.g.: half4(2) + 2 |
| 533 | if (leftType.isVector() && leftType.componentType() == rightType) { |
| 534 | if (rightType.isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 535 | return simplify_vector<SKSL_FLOAT>(context, *left, op, |
| 536 | splat_scalar(*right, left->type())); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 537 | } |
| 538 | if (rightType.isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 539 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, *left, op, |
| 540 | splat_scalar(*right, left->type())); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 541 | } |
| 542 | return nullptr; |
| 543 | } |
| 544 | |
| 545 | // Perform constant folding on scalars against vectors, e.g.: 2 + half4(2) |
| 546 | if (rightType.isVector() && rightType.componentType() == leftType) { |
| 547 | if (leftType.isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 548 | return simplify_vector<SKSL_FLOAT>(context, splat_scalar(*left, right->type()), op, |
| 549 | *right); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 550 | } |
| 551 | if (leftType.isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 552 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, splat_scalar(*left, right->type()), |
| 553 | op, *right); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 554 | } |
| 555 | return nullptr; |
| 556 | } |
| 557 | |
John Stiles | 22a5454 | 2021-03-31 11:33:48 -0400 | [diff] [blame] | 558 | // Perform constant folding on pairs of matrices or arrays. |
| 559 | if ((leftType.isMatrix() && rightType.isMatrix()) || |
| 560 | (leftType.isArray() && rightType.isArray())) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 561 | bool equality; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 562 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 563 | case Token::Kind::TK_EQEQ: |
| 564 | equality = true; |
| 565 | break; |
| 566 | case Token::Kind::TK_NEQ: |
| 567 | equality = false; |
| 568 | break; |
| 569 | default: |
| 570 | return nullptr; |
| 571 | } |
| 572 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame] | 573 | switch (left->compareConstant(*right)) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 574 | case Expression::ComparisonResult::kNotEqual: |
| 575 | equality = !equality; |
| 576 | [[fallthrough]]; |
| 577 | |
| 578 | case Expression::ComparisonResult::kEqual: |
John Stiles | 9ce80f7 | 2021-03-11 22:35:19 -0500 | [diff] [blame] | 579 | return BoolLiteral::Make(context, offset, equality); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 580 | |
| 581 | case Expression::ComparisonResult::kUnknown: |
| 582 | return nullptr; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | // We aren't able to constant-fold. |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 587 | return nullptr; |
| 588 | } |
| 589 | |
| 590 | } // namespace SkSL |