summaryrefslogtreecommitdiffstats
path: root/gfx/skia/skia/src/sksl/SkSLConstantFolder.h
blob: 25dd2e7b8675786bd899dd8c2ef51023f5d1278f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
 * Copyright 2020 Google LLC
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SKSL_CONSTANT_FOLDER
#define SKSL_CONSTANT_FOLDER

#include <memory>

#include "include/private/SkSLDefines.h"
#include "include/sksl/SkSLOperator.h"

namespace SkSL {

class Context;
class Expression;
class Position;
class Type;

/**
 * Performs constant folding on IR expressions. This simplifies expressions containing
 * compile-time constants, such as replacing `Literal(2) + Literal(2)` with `Literal(4)`.
 */
class ConstantFolder {
public:
    /**
     * If value is an int literal or const int variable with a known value, returns true and stores
     * the value in out. Otherwise returns false.
     */
    static bool GetConstantInt(const Expression& value, SKSL_INT* out);

    /**
     * If value is a literal or const scalar variable with a known value, returns true and stores
     * the value in out. Otherwise returns false.
     */
    static bool GetConstantValue(const Expression& value, double* out);

    /**
     * If the expression is a const variable with a known compile-time-constant value, returns that
     * value. If not, returns the original expression as-is.
     */
    static const Expression* GetConstantValueForVariable(const Expression& value);

    /**
     * If the expression is a const variable with a known compile-time-constant value, returns that
     * value. If not, returns null.
     */
    static const Expression* GetConstantValueOrNullForVariable(const Expression& value);

    /**
     * If the expression is a const variable with a known compile-time-constant value, returns a
     * clone of that value. If not, returns the original expression as-is.
     */
    static std::unique_ptr<Expression> MakeConstantValueForVariable(Position pos,
            std::unique_ptr<Expression> expr);

    /** Simplifies the binary expression `left OP right`. Returns null if it can't be simplified. */
    static std::unique_ptr<Expression> Simplify(const Context& context,
                                                Position pos,
                                                const Expression& left,
                                                Operator op,
                                                const Expression& right,
                                                const Type& resultType);
};

}  // namespace SkSL

#endif  // SKSL_CONSTANT_FOLDER