xref: /aosp_15_r20/external/skia/src/sksl/analysis/SkSLProgramUsage.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef SKSL_PROGRAMUSAGE
9 #define SKSL_PROGRAMUSAGE
10 
11 #include "include/core/SkTypes.h"
12 #include "src/core/SkTHash.h"
13 
14 namespace SkSL {
15 
16 class Expression;
17 class FunctionDeclaration;
18 class ProgramElement;
19 class Statement;
20 class Symbol;
21 class Variable;
22 
23 /**
24  * Side-car class holding mutable information about a Program's IR
25  */
26 class ProgramUsage {
27 public:
28     struct VariableCounts {
29         int fVarExists = 0;  // if this is zero, the Variable might have already been deleted
30         int fRead = 0;
31         int fWrite = 0;
32     };
33     VariableCounts get(const Variable&) const;
34     bool isDead(const Variable&) const;
35 
36     int get(const FunctionDeclaration&) const;
37 
38     void add(const Expression* expr);
39     void add(const Statement* stmt);
40     void add(const ProgramElement& element);
41     void remove(const Expression* expr);
42     void remove(const Statement* stmt);
43     void remove(const ProgramElement& element);
44 
45     bool operator==(const ProgramUsage& that) const;
46     bool operator!=(const ProgramUsage& that) const { return !(*this == that); }
47 
48     // All Symbol* objects in fStructCounts must be StructType*.
49     skia_private::THashMap<const Symbol*, int> fStructCounts;
50     // All Symbol* objects in fCallCounts must be FunctionDeclaration*.
51     skia_private::THashMap<const Symbol*, int> fCallCounts;
52     skia_private::THashMap<const Variable*, VariableCounts> fVariableCounts;
53 };
54 
55 }  // namespace SkSL
56 
57 #endif
58