blob: 6692dd222fb6c589be8b89822d60c493c053b7a7 [file] [log] [blame]
John Stilesdc8ec312021-01-11 11:05:21 -05001/*
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
Ethan Nicholasdaed2592021-03-04 14:30:25 -050013#include "include/private/SkSLDefines.h"
John Stiles45990502021-02-16 10:55:27 -050014#include "src/sksl/SkSLOperators.h"
John Stilesdc8ec312021-01-11 11:05:21 -050015
16namespace SkSL {
17
18class Context;
John Stilesdc8ec312021-01-11 11:05:21 -050019class Expression;
20
21/**
22 * Performs constant folding on IR expressions. This simplifies expressions containing
John Stiles7591d4b2021-09-13 13:32:06 -040023 * compile-time constants, such as replacing `Literal(2) + Literal(2)` with `Literal(4)`.
John Stilesdc8ec312021-01-11 11:05:21 -050024 */
25class ConstantFolder {
26public:
Ethan Nicholasc0f98152021-02-05 16:21:10 -050027 /**
28 * If value is an int literal or const int variable with a known value, returns true and stores
29 * the value in out. Otherwise returns false.
30 */
31 static bool GetConstantInt(const Expression& value, SKSL_INT* out);
32
33 /**
Brian Osman448b2d52021-09-23 11:36:15 -040034 * If value is a literal or const scalar variable with a known value, returns true and stores
35 * the value in out. Otherwise returns false.
36 */
37 static bool GetConstantValue(const Expression& value, double* out);
38
39 /**
John Stilese80e1692021-03-02 17:02:25 -050040 * If the expression is a const variable with a known compile-time-constant value, returns that
41 * value. If not, returns the original expression as-is.
42 */
43 static const Expression* GetConstantValueForVariable(const Expression& value);
44
45 /**
John Stilesffba5242021-05-07 10:50:22 -040046 * If the expression is a const variable with a known compile-time-constant value, returns a
47 * clone of that value. If not, returns the original expression as-is.
48 */
49 static std::unique_ptr<Expression> MakeConstantValueForVariable(
50 std::unique_ptr<Expression> expr);
51
52 /**
Ethan Nicholasc0f98152021-02-05 16:21:10 -050053 * Reports an error and returns true if op is a division / mod operator and right is zero or
54 * contains a zero element.
55 */
Ethan Nicholas89cfde12021-09-27 11:20:34 -040056 static bool ErrorOnDivideByZero(const Context& context, int line, Operator op,
Ethan Nicholasc0f98152021-02-05 16:21:10 -050057 const Expression& right);
58
John Stilesdc8ec312021-01-11 11:05:21 -050059 /** Simplifies the binary expression `left OP right`. Returns null if it can't be simplified. */
60 static std::unique_ptr<Expression> Simplify(const Context& context,
Ethan Nicholas89cfde12021-09-27 11:20:34 -040061 int line,
John Stilesdc8ec312021-01-11 11:05:21 -050062 const Expression& left,
John Stiles45990502021-02-16 10:55:27 -050063 Operator op,
John Stiles8f440b42021-03-05 16:48:56 -050064 const Expression& right,
65 const Type& resultType);
John Stilesdc8ec312021-01-11 11:05:21 -050066};
67
68} // namespace SkSL
69
70#endif // SKSL_CONSTANT_FOLDER