xref: /aosp_15_r20/external/skia/resources/sksl/shared/Optimizations.sksl (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1/*#pragma settings NoOptimize*/
2
3uniform half4 colorGreen, colorRed;
4
5bool flatten_compound_constructor() {
6    int4 x = int4(int3(int2(1, 2), 3), 4);
7    int4 y = int4(1, int3(2, int2(3, 4)));
8    return x == y;
9}
10
11bool flatten_known_if() {
12    int value;
13    if (true) {
14        value = 1;
15    } else {
16        value = 2;
17    }
18    return value == 1;
19}
20
21bool eliminate_empty_if_else() {
22    bool check = false;
23    if (check = !check /* assignment is intentional! */) {} else {}
24    return check;
25}
26
27bool eliminate_empty_else() {
28    bool check = true;
29    if (check) { return true; } else {}
30    return false;
31}
32
33bool flatten_matching_ternary() {
34    bool check = true;
35    return check ? true : true;
36}
37
38bool flatten_expr_without_side_effects() {
39    bool check = true;
40    check;
41    return check;
42}
43
44bool eliminate_no_op_arithmetic() {
45    // Constant-expression folding needs to work when all values are known, even if optimizations
46    // are disabled.
47    const int ONE = 1;
48    int a1[ONE * 1];
49    int a2[ONE + 0];
50
51    // However, expressions with a known and an unknown shouldn't fold when optimizations are off.
52    int x = ONE;
53    x = x + 0;
54    x *= 1;
55    return x == 1;
56}
57
58bool flatten_switch() {
59    switch (1) {
60        case 0: return false;
61        case 1: return true;
62        case 2: return false;
63    }
64    return false;
65}
66
67half4 main(float2 coords) {
68    return flatten_compound_constructor()
69           && flatten_known_if()
70           && eliminate_empty_if_else()
71           && eliminate_empty_else()
72           && flatten_matching_ternary()
73           && flatten_expr_without_side_effects()
74           && eliminate_no_op_arithmetic()
75           && flatten_switch()
76           ? colorGreen : colorRed;
77}
78