Debian Codegraph @

src/sksl/transform/SkSLAddConstToVarModifiers.cpp

raw
1 /*
2 * Copyright 2022 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 #include "src/base/SkEnumBitMask.h"
9 #include "src/sksl/SkSLAnalysis.h"
10 #include "src/sksl/analysis/SkSLProgramUsage.h"
11 #include "src/sksl/ir/SkSLModifierFlags.h"
12 #include "src/sksl/ir/SkSLVariable.h"
13 #include "src/sksl/transform/SkSLTransform.h"
14
15 namespace SkSL {
16
17 class Expression;
18
19 ModifierFlags Transform::AddConstToVarModifiers(const Variable& var,
20 const Expression* initialValue,
21 const ProgramUsage* usage) {
22 // If the variable is already marked as `const`, keep our existing modifiers.
23 ModifierFlags flags = var.modifierFlags();
24 if (flags.isConst()) {
25 return flags;
26 }
27 // If the variable doesn't have a compile-time-constant initial value, we can't `const` it.
28 if (!initialValue || !Analysis::IsCompileTimeConstant(*initialValue)) {
29 return flags;
30 }
31 // This only works for variables that are written-to a single time.
32 ProgramUsage::VariableCounts counts = usage->get(var);
33 if (counts.fWrite != 1) {
34 return flags;
35 }
36 // Add `const` to our variable's modifier flags, making it eligible for constant-folding.
37 return flags | ModifierFlag::kConst;
38 }
39
40 } // namespace SkSL