2 * Copyright 2020 Google LLC
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SKSL_CONSTANT_FOLDER
9 #define SKSL_CONSTANT_FOLDER
13 #include "include/private/SkSLDefines.h"
14 #include "include/sksl/SkSLOperator.h"
24 * Performs constant folding on IR expressions. This simplifies expressions containing
25 * compile-time constants, such as replacing `Literal(2) + Literal(2)` with `Literal(4)`.
27 class ConstantFolder {
30 * If value is an int literal or const int variable with a known value, returns true and stores
31 * the value in out. Otherwise returns false.
33 static bool GetConstantInt(const Expression& value, SKSL_INT* out);
36 * If value is a literal or const scalar variable with a known value, returns true and stores
37 * the value in out. Otherwise returns false.
39 static bool GetConstantValue(const Expression& value, double* out);
42 * If the expression is a const variable with a known compile-time-constant value, returns that
43 * value. If not, returns the original expression as-is.
45 static const Expression* GetConstantValueForVariable(const Expression& value);
48 * If the expression is a const variable with a known compile-time-constant value, returns a
49 * clone of that value. If not, returns the original expression as-is.
51 static std::unique_ptr<Expression> MakeConstantValueForVariable(Position pos,
52 std::unique_ptr<Expression> expr);
54 /** Simplifies the binary expression `left OP right`. Returns null if it can't be simplified. */
55 static std::unique_ptr<Expression> Simplify(const Context& context,
57 const Expression& left,
59 const Expression& right,
60 const Type& resultType);
65 #endif // SKSL_CONSTANT_FOLDER