intel/compiler: Introduce simple IR analysis pass framework
[mesa.git] / src / intel / compiler / brw_ir_analysis.h
1 /* -*- c++ -*- */
2 /*
3 * Copyright © 2016 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #ifndef BRW_IR_ANALYSIS_H
26 #define BRW_IR_ANALYSIS_H
27
28 namespace brw {
29 /**
30 * Bitset of state categories that can influence the result of IR analysis
31 * passes.
32 */
33 enum analysis_dependency_class {
34 /**
35 * The analysis doesn't depend on the IR, its result is effectively a
36 * constant during the compilation.
37 */
38 DEPENDENCY_NOTHING = 0,
39 /**
40 * The analysis depends on the program being literally the same (good
41 * luck...), any change in the input invalidates previous analysis
42 * computations.
43 */
44 DEPENDENCY_EVERYTHING = ~0
45 };
46
47 inline analysis_dependency_class
48 operator|(analysis_dependency_class x, analysis_dependency_class y)
49 {
50 return static_cast<analysis_dependency_class>(
51 static_cast<unsigned>(x) | static_cast<unsigned>(y));
52 }
53 }
54
55 /**
56 * Instantiate a program analysis class \p L which can calculate an object of
57 * type \p T as result. \p C is a closure that encapsulates whatever
58 * information is required as argument to run the analysis pass. The purpose
59 * of this class is to make sure that:
60 *
61 * - The analysis pass is executed lazily whenever it's needed and multiple
62 * executions are optimized out as long as the cached result remains marked
63 * up-to-date.
64 *
65 * - There is no way to access the cached analysis result without first
66 * calling L::require(), which makes sure that the analysis pass is rerun
67 * if necessary.
68 *
69 * - The cached result doesn't become inconsistent with the program for as
70 * long as it remains marked up-to-date. (This is only enforced in debug
71 * builds for performance reasons)
72 *
73 * The requirements on \p T are the following:
74 *
75 * - Constructible with a single argument, as in 'x = T(c)' for \p c of type
76 * \p C.
77 *
78 * - 'x.dependency_class()' on const \p x returns a bitset of
79 * brw::analysis_dependency_class specifying the set of IR objects that are
80 * required to remain invariant for the cached analysis result to be
81 * considered valid.
82 *
83 * - 'x.validate(c)' on const \p x returns a boolean result specifying
84 * whether the analysis result \p x is consistent with the input IR. This
85 * is currently only used for validation in debug builds.
86 */
87 #define BRW_ANALYSIS(L, T, C) \
88 class L { \
89 public: \
90 /** \
91 * Construct a program analysis. \p c is an arbitrary object \
92 * passed as argument to the constructor of the analysis result \
93 * object of type \p T. \
94 */ \
95 L(C const &c) : c(c), p(NULL) {} \
96 \
97 /** \
98 * Destroy a program analysis. \
99 */ \
100 ~L() \
101 { \
102 delete p; \
103 } \
104 \
105 /** \
106 * Obtain the result of a program analysis. This gives a \
107 * guaranteed up-to-date result, the analysis pass will be \
108 * rerun implicitly if it has become stale. \
109 */ \
110 T & \
111 require() \
112 { \
113 if (p) \
114 assert(p->validate(c)); \
115 else \
116 p = new T(c); \
117 \
118 return *p; \
119 } \
120 \
121 const T & \
122 require() const \
123 { \
124 return const_cast<L *>(this)->require(); \
125 } \
126 \
127 /** \
128 * Report that dependencies of the analysis pass may have changed \
129 * since the last calculation and the cached analysis result may \
130 * have to be discarded. \
131 */ \
132 void \
133 invalidate(brw::analysis_dependency_class c) \
134 { \
135 if (p && c & p->dependency_class()) { \
136 delete p; \
137 p = NULL; \
138 } \
139 } \
140 \
141 private: \
142 C c; \
143 T *p; \
144 }
145
146 #endif