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