• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
26 
27 namespace brw {
28    /**
29     * Bitset of state categories that can influence the result of IR analysis
30     * passes.
31     */
32    enum analysis_dependency_class {
33       /**
34        * The analysis doesn't depend on the IR, its result is effectively a
35        * constant during the compilation.
36        */
37       DEPENDENCY_NOTHING = 0,
38       /**
39        * The analysis depends on the set of instructions in the program and
40        * their naming.  Note that because instructions are named sequentially
41        * by IP this implies a dependency on the control flow edges between
42        * instructions.  This will be signaled whenever instructions are
43        * inserted, removed or reordered in the program.
44        */
45       DEPENDENCY_INSTRUCTION_IDENTITY = 0x1,
46       /**
47        * The analysis is sensitive to the detailed semantics of instructions
48        * in the program, where "detailed" means any change in the instruction
49        * data structures other than the linked-list pointers (which are
50        * already covered by DEPENDENCY_INSTRUCTION_IDENTITY).  E.g. changing
51        * the negate or abs flags of an instruction source would signal this
52        * flag alone because it would preserve all other instruction dependency
53        * classes.
54        */
55       DEPENDENCY_INSTRUCTION_DETAIL = 0x2,
56       /**
57        * The analysis depends on the set of data flow edges between
58        * instructions.  This will be signaled whenever the dataflow relation
59        * between instructions has potentially changed, e.g. when the VGRF
60        * index of an instruction source or destination changes (in which case
61        * it will appear in combination with DEPENDENCY_INSTRUCTION_DETAIL), or
62        * when data-dependent instructions are reordered (in which case it will
63        * appear in combination with DEPENDENCY_INSTRUCTION_IDENTITY).
64        */
65       DEPENDENCY_INSTRUCTION_DATA_FLOW = 0x4,
66       /**
67        * The analysis depends on all instruction dependency classes.  These
68        * will typically be signaled simultaneously when inserting or removing
69        * instructions in the program (or if you're feeling too lazy to read
70        * through your optimization pass to figure out which of the instruction
71        * dependency classes above it invalidates).
72        */
73       DEPENDENCY_INSTRUCTIONS = 0x7,
74       /**
75        * The analysis depends on the set of VGRFs in the program and their
76        * naming.  This will be signaled when VGRFs are allocated or released.
77        */
78       DEPENDENCY_VARIABLES = 0x8,
79       /**
80        * The analysis depends on the set of basic blocks in the program, their
81        * control flow edges and naming.
82        */
83       DEPENDENCY_BLOCKS = 0x10,
84       /**
85        * The analysis depends on the program being literally the same (good
86        * luck...), any change in the input invalidates previous analysis
87        * computations.
88        */
89       DEPENDENCY_EVERYTHING = ~0
90    };
91 
92    inline analysis_dependency_class
93    operator|(analysis_dependency_class x, analysis_dependency_class y)
94    {
95       return static_cast<analysis_dependency_class>(
96          static_cast<unsigned>(x) | static_cast<unsigned>(y));
97    }
98 }
99 
100 /**
101  * Instantiate a program analysis class \p L which can calculate an object of
102  * type \p T as result.  \p C is a closure that encapsulates whatever
103  * information is required as argument to run the analysis pass.  The purpose
104  * of this class is to make sure that:
105  *
106  *  - The analysis pass is executed lazily whenever it's needed and multiple
107  *    executions are optimized out as long as the cached result remains marked
108  *    up-to-date.
109  *
110  *  - There is no way to access the cached analysis result without first
111  *    calling L::require(), which makes sure that the analysis pass is rerun
112  *    if necessary.
113  *
114  *  - The cached result doesn't become inconsistent with the program for as
115  *    long as it remains marked up-to-date. (This is only enforced in debug
116  *    builds for performance reasons)
117  *
118  * The requirements on \p T are the following:
119  *
120  *  - Constructible with a single argument, as in 'x = T(c)' for \p c of type
121  *    \p C.
122  *
123  *  - 'x.dependency_class()' on const \p x returns a bitset of
124  *    brw::analysis_dependency_class specifying the set of IR objects that are
125  *    required to remain invariant for the cached analysis result to be
126  *    considered valid.
127  *
128  *  - 'x.validate(c)' on const \p x returns a boolean result specifying
129  *    whether the analysis result \p x is consistent with the input IR.  This
130  *    is currently only used for validation in debug builds.
131  */
132 template<class T, class C>
133 class brw_analysis {
134 public:
135    /**
136     * Construct a program analysis.  \p c is an arbitrary object
137     * passed as argument to the constructor of the analysis result
138     * object of type \p T.
139     */
brw_analysis(const C * c)140    brw_analysis(const C *c) : c(c), p(NULL) {}
141 
142    /**
143     * Destroy a program analysis.
144     */
~brw_analysis()145    ~brw_analysis()
146    {
147       delete p;
148    }
149 
150    /**
151     * Obtain the result of a program analysis.  This gives a
152     * guaranteed up-to-date result, the analysis pass will be
153     * rerun implicitly if it has become stale.
154     */
155    T &
require()156    require()
157    {
158       if (p)
159          assert(p->validate(c));
160       else
161          p = new T(c);
162 
163       return *p;
164    }
165 
166    const T &
require()167    require() const
168    {
169       return const_cast<brw_analysis<T, C> *>(this)->require();
170    }
171 
172    /**
173     * Report that dependencies of the analysis pass may have changed
174     * since the last calculation and the cached analysis result may
175     * have to be discarded.
176     */
177    void
invalidate(brw::analysis_dependency_class c)178    invalidate(brw::analysis_dependency_class c)
179    {
180       if (p && (c & p->dependency_class())) {
181          delete p;
182          p = NULL;
183       }
184    }
185 
186 private:
187    const C *c;
188    T *p;
189 };
190