Flutter Engine
The Flutter Engine
SkSLConstantFolder.h
Go to the documentation of this file.
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#ifndef SKSL_CONSTANT_FOLDER
9#define SKSL_CONSTANT_FOLDER
10
11#include <memory>
12
15
16namespace SkSL {
17
18class Context;
19class Expression;
20class Position;
21class Type;
22
23/**
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)`.
26 */
28public:
29 /**
30 * If `value` is an int literal or const int variable with a known value, returns true and
31 * stores the value in `out`. Otherwise, returns false.
32 */
33 static bool GetConstantInt(const Expression& value, SKSL_INT* out);
34
35 /**
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.
38 */
39 static bool GetConstantValue(const Expression& value, double* out);
40
41 /**
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.
44 */
46
47 /**
48 * If the expression can be replaced by a compile-time-constant value, returns that value.
49 * If not, returns null.
50 */
52
53 /** Returns true if the expression contains `value` in every slot. */
54 static bool IsConstantSplat(const Expression& expr, double value);
55
56 /**
57 * If the expression is a const variable with a known compile-time-constant value, returns a
58 * clone of that value. If not, returns the original expression as-is.
59 */
60 static std::unique_ptr<Expression> MakeConstantValueForVariable(Position pos,
61 std::unique_ptr<Expression> expr);
62
63 /** Simplifies the binary expression `left OP right`. Returns null if it can't be simplified. */
64 static std::unique_ptr<Expression> Simplify(const Context& context,
66 const Expression& left,
67 Operator op,
68 const Expression& right,
69 const Type& resultType);
70};
71
72} // namespace SkSL
73
74#endif // SKSL_CONSTANT_FOLDER
SkPoint pos
int64_t SKSL_INT
Definition: SkSLDefines.h:16
static bool GetConstantValue(const Expression &value, double *out)
static bool GetConstantInt(const Expression &value, SKSL_INT *out)
static std::unique_ptr< Expression > Simplify(const Context &context, Position pos, const Expression &left, Operator op, const Expression &right, const Type &resultType)
static bool IsConstantSplat(const Expression &expr, double value)
static const Expression * GetConstantValueOrNull(const Expression &value)
static const Expression * GetConstantValueForVariable(const Expression &value)
static std::unique_ptr< Expression > MakeConstantValueForVariable(Position pos, std::unique_ptr< Expression > expr)
uint8_t value