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" |
| 20 | #include "src/sksl/ir/SkSLType.h" |
| 21 | #include "src/sksl/ir/SkSLVariable.h" |
| 22 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 23 | |
| 24 | namespace SkSL { |
| 25 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 26 | static std::unique_ptr<Expression> eliminate_no_op_boolean(const Expression& left, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 27 | Operator op, |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 28 | const Expression& right) { |
| 29 | SkASSERT(right.is<BoolLiteral>()); |
| 30 | bool rightVal = right.as<BoolLiteral>().value(); |
| 31 | |
| 32 | // Detect no-op Boolean expressions and optimize them away. |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 33 | if ((op.kind() == Token::Kind::TK_LOGICALAND && rightVal) || // (expr && true) -> (expr) |
| 34 | (op.kind() == Token::Kind::TK_LOGICALOR && !rightVal) || // (expr || false) -> (expr) |
| 35 | (op.kind() == Token::Kind::TK_LOGICALXOR && !rightVal) || // (expr ^^ false) -> (expr) |
| 36 | (op.kind() == Token::Kind::TK_EQEQ && rightVal) || // (expr == true) -> (expr) |
| 37 | (op.kind() == Token::Kind::TK_NEQ && !rightVal)) { // (expr != false) -> (expr) |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 38 | |
| 39 | return left.clone(); |
| 40 | } |
| 41 | |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 45 | static std::unique_ptr<Expression> short_circuit_boolean(const Expression& left, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 46 | Operator op, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 47 | const Expression& right) { |
| 48 | SkASSERT(left.is<BoolLiteral>()); |
| 49 | bool leftVal = left.as<BoolLiteral>().value(); |
| 50 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 51 | // 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] | 52 | if ((op.kind() == Token::Kind::TK_LOGICALAND && !leftVal) || // (false && expr) -> (false) |
| 53 | (op.kind() == Token::Kind::TK_LOGICALOR && leftVal)) { // (true || expr) -> (true) |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 54 | |
| 55 | return left.clone(); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 56 | } |
| 57 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 58 | // We can't eliminate the right-side expression via short-circuit, but we might still be able to |
| 59 | // simplify away a no-op expression. |
| 60 | return eliminate_no_op_boolean(right, op, left); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 63 | // 'T' is the actual stored type of the literal data (SKSL_FLOAT or SKSL_INT). |
| 64 | // 'U' is an unsigned version of that, used to perform addition, subtraction, and multiplication, |
| 65 | // to avoid signed-integer overflow errors. This mimics the use of URESULT vs. RESULT when doing |
| 66 | // scalar folding in Simplify, later in this file. |
| 67 | template <typename T, typename U = T> |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 68 | static std::unique_ptr<Expression> simplify_vector(const Context& context, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 69 | const Expression& left, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 70 | Operator op, |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 71 | const Expression& right) { |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 72 | SkASSERT(left.type().isVector()); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 73 | SkASSERT(left.type() == right.type()); |
| 74 | const Type& type = left.type(); |
| 75 | |
| 76 | // Handle boolean operations: == != |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 77 | if (op.kind() == Token::Kind::TK_EQEQ || op.kind() == Token::Kind::TK_NEQ) { |
| 78 | bool equality = (op.kind() == Token::Kind::TK_EQEQ); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 79 | |
| 80 | switch (left.compareConstant(right)) { |
| 81 | case Expression::ComparisonResult::kNotEqual: |
| 82 | equality = !equality; |
| 83 | [[fallthrough]]; |
| 84 | |
| 85 | case Expression::ComparisonResult::kEqual: |
| 86 | return std::make_unique<BoolLiteral>(context, left.fOffset, equality); |
| 87 | |
| 88 | case Expression::ComparisonResult::kUnknown: |
| 89 | return nullptr; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Handle floating-point arithmetic: + - * / |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 94 | const auto vectorComponentwiseFold = [&](auto foldFn) -> std::unique_ptr<Expression> { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 95 | const Type& componentType = type.componentType(); |
| 96 | ExpressionArray args; |
| 97 | args.reserve_back(type.columns()); |
| 98 | for (int i = 0; i < type.columns(); i++) { |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 99 | U value = foldFn(left.getVecComponent<T>(i), right.getVecComponent<T>(i)); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 100 | args.push_back(std::make_unique<Literal<T>>(left.fOffset, value, &componentType)); |
| 101 | } |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 102 | return Constructor::Make(context, left.fOffset, type, std::move(args)); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 105 | switch (op.kind()) { |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 106 | case Token::Kind::TK_PLUS: return vectorComponentwiseFold([](U a, U b) { return a + b; }); |
| 107 | case Token::Kind::TK_MINUS: return vectorComponentwiseFold([](U a, U b) { return a - b; }); |
| 108 | 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] | 109 | 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] | 110 | default: |
| 111 | return nullptr; |
| 112 | } |
| 113 | } |
| 114 | |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 115 | static Constructor splat_scalar(const Expression& scalar, const Type& type) { |
| 116 | SkASSERT(type.isVector()); |
| 117 | SkASSERT(type.componentType() == scalar.type()); |
| 118 | |
| 119 | // Use a Constructor to splat the scalar expression across a vector. |
| 120 | ExpressionArray arg; |
| 121 | arg.push_back(scalar.clone()); |
John Stiles | 54f0049 | 2021-02-19 11:46:10 -0500 | [diff] [blame] | 122 | return Constructor{scalar.fOffset, type, std::move(arg)}; |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 125 | bool ConstantFolder::GetConstantInt(const Expression& value, SKSL_INT* out) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 126 | const Expression* expr = GetConstantValueForVariable(value); |
| 127 | if (!expr->is<IntLiteral>()) { |
| 128 | return false; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 129 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 130 | *out = expr->as<IntLiteral>().value(); |
| 131 | return true; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | bool ConstantFolder::GetConstantFloat(const Expression& value, SKSL_FLOAT* out) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 135 | const Expression* expr = GetConstantValueForVariable(value); |
| 136 | if (!expr->is<FloatLiteral>()) { |
| 137 | return false; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 138 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 139 | *out = expr->as<FloatLiteral>().value(); |
| 140 | return true; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static bool contains_constant_zero(const Expression& expr) { |
| 144 | if (expr.is<Constructor>()) { |
| 145 | for (const auto& arg : expr.as<Constructor>().arguments()) { |
| 146 | if (contains_constant_zero(*arg)) { |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | return false; |
| 151 | } |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 152 | const Expression* value = ConstantFolder::GetConstantValueForVariable(expr); |
| 153 | return (value->is<IntLiteral>() && value->as<IntLiteral>().value() == 0.0) || |
| 154 | (value->is<FloatLiteral>() && value->as<FloatLiteral>().value() == 0.0); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 155 | } |
| 156 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 157 | bool ConstantFolder::ErrorOnDivideByZero(const Context& context, int offset, Operator op, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 158 | const Expression& right) { |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 159 | switch (op.kind()) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 160 | case Token::Kind::TK_SLASH: |
| 161 | case Token::Kind::TK_SLASHEQ: |
| 162 | case Token::Kind::TK_PERCENT: |
| 163 | case Token::Kind::TK_PERCENTEQ: |
| 164 | if (contains_constant_zero(right)) { |
| 165 | context.fErrors.error(offset, "division by zero"); |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | default: |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 174 | const Expression* ConstantFolder::GetConstantValueForVariable(const Expression& inExpr) { |
| 175 | for (const Expression* expr = &inExpr;;) { |
| 176 | if (!expr->is<VariableReference>()) { |
| 177 | break; |
| 178 | } |
| 179 | const VariableReference& varRef = expr->as<VariableReference>(); |
| 180 | if (varRef.refKind() != VariableRefKind::kRead) { |
| 181 | break; |
| 182 | } |
| 183 | const Variable& var = *varRef.variable(); |
| 184 | if (!(var.modifiers().fFlags & Modifiers::kConst_Flag)) { |
| 185 | break; |
| 186 | } |
| 187 | expr = var.initialValue(); |
| 188 | SkASSERT(expr); |
| 189 | if (expr->isCompileTimeConstant()) { |
| 190 | return expr; |
| 191 | } |
| 192 | if (!expr->is<VariableReference>()) { |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | // We didn't find a compile-time constant at the end. Return the expression as-is. |
| 197 | return &inExpr; |
| 198 | } |
| 199 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 200 | std::unique_ptr<Expression> ConstantFolder::Simplify(const Context& context, |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 201 | int offset, |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 202 | const Expression& leftExpr, |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 203 | Operator op, |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 204 | const Expression& rightExpr) { |
| 205 | // Replace constant variables with trivial initial-values. |
| 206 | const Expression* left = GetConstantValueForVariable(leftExpr); |
| 207 | const Expression* right = GetConstantValueForVariable(rightExpr); |
| 208 | |
John Stiles | abc3b78 | 2021-02-05 10:07:19 -0500 | [diff] [blame] | 209 | // If this is the comma operator, the left side is evaluated but not otherwise used in any way. |
| 210 | // 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^] | 211 | if (op.kind() == Token::Kind::TK_COMMA && !left->hasSideEffects()) { |
| 212 | return right->clone(); |
John Stiles | abc3b78 | 2021-02-05 10:07:19 -0500 | [diff] [blame] | 213 | } |
| 214 | |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 215 | // If this is the assignment operator, and both sides are the same trivial expression, this is |
| 216 | // self-assignment (i.e., `var = var`) and can be reduced to just a variable reference (`var`). |
| 217 | // This can happen when other parts of the assignment are optimized away. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 218 | if (op.kind() == Token::Kind::TK_EQ && Analysis::IsSelfAssignment(*left, *right)) { |
| 219 | return right->clone(); |
John Stiles | 95d0bad | 2021-03-01 17:02:28 -0500 | [diff] [blame] | 220 | } |
| 221 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 222 | // Simplify the expression when both sides are constant Boolean literals. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 223 | if (left->is<BoolLiteral>() && right->is<BoolLiteral>()) { |
| 224 | bool leftVal = left->as<BoolLiteral>().value(); |
| 225 | bool rightVal = right->as<BoolLiteral>().value(); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 226 | bool result; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 227 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 228 | case Token::Kind::TK_LOGICALAND: result = leftVal && rightVal; break; |
| 229 | case Token::Kind::TK_LOGICALOR: result = leftVal || rightVal; break; |
| 230 | case Token::Kind::TK_LOGICALXOR: result = leftVal ^ rightVal; break; |
John Stiles | 26fdcbb | 2021-01-19 19:00:31 -0500 | [diff] [blame] | 231 | case Token::Kind::TK_EQEQ: result = leftVal == rightVal; break; |
| 232 | case Token::Kind::TK_NEQ: result = leftVal != rightVal; break; |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 233 | default: return nullptr; |
| 234 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 235 | return std::make_unique<BoolLiteral>(context, offset, result); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 236 | } |
| 237 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 238 | // If the left side is a Boolean literal, apply short-circuit optimizations. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 239 | if (left->is<BoolLiteral>()) { |
| 240 | return short_circuit_boolean(*left, op, *right); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // If the right side is a Boolean literal... |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 244 | if (right->is<BoolLiteral>()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 245 | // ... and the left side has no side effects... |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 246 | if (!left->hasSideEffects()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 247 | // We can reverse the expressions and short-circuit optimizations are still valid. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 248 | return short_circuit_boolean(*right, op, *left); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // 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^] | 252 | return eliminate_no_op_boolean(*left, op, *right); |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 253 | } |
| 254 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 255 | if (ErrorOnDivideByZero(context, offset, op, *right)) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 256 | return nullptr; |
| 257 | } |
| 258 | |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 259 | // Other than the short-circuit cases above, constant folding requires both sides to be constant |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 260 | if (!left->isCompileTimeConstant() || !right->isCompileTimeConstant()) { |
John Stiles | 8a9da73 | 2021-01-20 14:32:33 -0500 | [diff] [blame] | 261 | return nullptr; |
| 262 | } |
| 263 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 264 | // Note that we expressly do not worry about precision and overflow here -- we use the maximum |
| 265 | // precision to calculate the results and hope the result makes sense. |
| 266 | // TODO: detect and handle integer overflow properly. |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 267 | using SKSL_UINT = uint64_t; |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 268 | #define RESULT(t, op) std::make_unique<t ## Literal>(context, offset, \ |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 269 | leftVal op rightVal) |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 270 | #define URESULT(t, op) std::make_unique<t ## Literal>(context, offset, \ |
Brian Osman | 21d2b6a | 2021-02-01 13:48:50 -0500 | [diff] [blame] | 271 | (SKSL_UINT) leftVal op \ |
| 272 | (SKSL_UINT) rightVal) |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 273 | if (left->is<IntLiteral>() && right->is<IntLiteral>()) { |
| 274 | SKSL_INT leftVal = left->as<IntLiteral>().value(); |
| 275 | SKSL_INT rightVal = right->as<IntLiteral>().value(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 276 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 277 | case Token::Kind::TK_PLUS: return URESULT(Int, +); |
| 278 | case Token::Kind::TK_MINUS: return URESULT(Int, -); |
| 279 | case Token::Kind::TK_STAR: return URESULT(Int, *); |
| 280 | case Token::Kind::TK_SLASH: |
| 281 | if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 282 | context.fErrors.error(offset, "arithmetic overflow"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 283 | return nullptr; |
| 284 | } |
| 285 | return RESULT(Int, /); |
| 286 | case Token::Kind::TK_PERCENT: |
| 287 | if (leftVal == std::numeric_limits<SKSL_INT>::min() && rightVal == -1) { |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 288 | context.fErrors.error(offset, "arithmetic overflow"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 289 | return nullptr; |
| 290 | } |
| 291 | return RESULT(Int, %); |
| 292 | case Token::Kind::TK_BITWISEAND: return RESULT(Int, &); |
| 293 | case Token::Kind::TK_BITWISEOR: return RESULT(Int, |); |
| 294 | case Token::Kind::TK_BITWISEXOR: return RESULT(Int, ^); |
| 295 | case Token::Kind::TK_EQEQ: return RESULT(Bool, ==); |
| 296 | case Token::Kind::TK_NEQ: return RESULT(Bool, !=); |
| 297 | case Token::Kind::TK_GT: return RESULT(Bool, >); |
| 298 | case Token::Kind::TK_GTEQ: return RESULT(Bool, >=); |
| 299 | case Token::Kind::TK_LT: return RESULT(Bool, <); |
| 300 | case Token::Kind::TK_LTEQ: return RESULT(Bool, <=); |
| 301 | case Token::Kind::TK_SHL: |
| 302 | if (rightVal >= 0 && rightVal <= 31) { |
Brian Osman | a7eb681 | 2021-02-01 11:43:05 -0500 | [diff] [blame] | 303 | // Left-shifting a negative (or really, any signed) value is undefined behavior |
| 304 | // in C++, but not GLSL. Do the shift on unsigned values, to avoid UBSAN. |
| 305 | return URESULT(Int, <<); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 306 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 307 | context.fErrors.error(offset, "shift value out of range"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 308 | return nullptr; |
| 309 | case Token::Kind::TK_SHR: |
| 310 | if (rightVal >= 0 && rightVal <= 31) { |
| 311 | return RESULT(Int, >>); |
| 312 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 313 | context.fErrors.error(offset, "shift value out of range"); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 314 | return nullptr; |
| 315 | |
| 316 | default: |
| 317 | return nullptr; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // Perform constant folding on pairs of floating-point literals. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 322 | if (left->is<FloatLiteral>() && right->is<FloatLiteral>()) { |
| 323 | SKSL_FLOAT leftVal = left->as<FloatLiteral>().value(); |
| 324 | SKSL_FLOAT rightVal = right->as<FloatLiteral>().value(); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 325 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 326 | case Token::Kind::TK_PLUS: return RESULT(Float, +); |
| 327 | case Token::Kind::TK_MINUS: return RESULT(Float, -); |
| 328 | case Token::Kind::TK_STAR: return RESULT(Float, *); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 329 | case Token::Kind::TK_SLASH: return RESULT(Float, /); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 330 | case Token::Kind::TK_EQEQ: return RESULT(Bool, ==); |
| 331 | case Token::Kind::TK_NEQ: return RESULT(Bool, !=); |
| 332 | case Token::Kind::TK_GT: return RESULT(Bool, >); |
| 333 | case Token::Kind::TK_GTEQ: return RESULT(Bool, >=); |
| 334 | case Token::Kind::TK_LT: return RESULT(Bool, <); |
| 335 | case Token::Kind::TK_LTEQ: return RESULT(Bool, <=); |
| 336 | default: return nullptr; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // Perform constant folding on pairs of vectors. |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 341 | const Type& leftType = left->type(); |
| 342 | const Type& rightType = right->type(); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 343 | if (leftType.isVector() && leftType == rightType) { |
| 344 | if (leftType.componentType().isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 345 | return simplify_vector<SKSL_FLOAT>(context, *left, op, *right); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 346 | } |
| 347 | if (leftType.componentType().isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 348 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, *left, op, *right); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 349 | } |
| 350 | return nullptr; |
| 351 | } |
| 352 | |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 353 | // Perform constant folding on vectors against scalars, e.g.: half4(2) + 2 |
| 354 | if (leftType.isVector() && leftType.componentType() == rightType) { |
| 355 | if (rightType.isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 356 | return simplify_vector<SKSL_FLOAT>(context, *left, op, |
| 357 | splat_scalar(*right, left->type())); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 358 | } |
| 359 | if (rightType.isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 360 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, *left, op, |
| 361 | splat_scalar(*right, left->type())); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 362 | } |
| 363 | return nullptr; |
| 364 | } |
| 365 | |
| 366 | // Perform constant folding on scalars against vectors, e.g.: 2 + half4(2) |
| 367 | if (rightType.isVector() && rightType.componentType() == leftType) { |
| 368 | if (leftType.isFloat()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 369 | return simplify_vector<SKSL_FLOAT>(context, splat_scalar(*left, right->type()), op, |
| 370 | *right); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 371 | } |
| 372 | if (leftType.isInteger()) { |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 373 | return simplify_vector<SKSL_INT, SKSL_UINT>(context, splat_scalar(*left, right->type()), |
| 374 | op, *right); |
John Stiles | 508eba7 | 2021-01-11 13:07:47 -0500 | [diff] [blame] | 375 | } |
| 376 | return nullptr; |
| 377 | } |
| 378 | |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 379 | // Perform constant folding on pairs of matrices. |
| 380 | if (leftType.isMatrix() && rightType.isMatrix()) { |
| 381 | bool equality; |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 382 | switch (op.kind()) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 383 | case Token::Kind::TK_EQEQ: |
| 384 | equality = true; |
| 385 | break; |
| 386 | case Token::Kind::TK_NEQ: |
| 387 | equality = false; |
| 388 | break; |
| 389 | default: |
| 390 | return nullptr; |
| 391 | } |
| 392 | |
John Stiles | e80e169 | 2021-03-02 17:02:25 -0500 | [diff] [blame^] | 393 | switch (left->compareConstant(*right)) { |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 394 | case Expression::ComparisonResult::kNotEqual: |
| 395 | equality = !equality; |
| 396 | [[fallthrough]]; |
| 397 | |
| 398 | case Expression::ComparisonResult::kEqual: |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 399 | return std::make_unique<BoolLiteral>(context, offset, equality); |
John Stiles | dc8ec31 | 2021-01-11 11:05:21 -0500 | [diff] [blame] | 400 | |
| 401 | case Expression::ComparisonResult::kUnknown: |
| 402 | return nullptr; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // We aren't able to constant-fold. |
| 407 | #undef RESULT |
| 408 | #undef URESULT |
| 409 | return nullptr; |
| 410 | } |
| 411 | |
| 412 | } // namespace SkSL |