aboutsummaryrefslogtreecommitdiffstats
path: root/src/intel/compiler/brw_ir_analysis.h
blob: 360b2f707356c27de111e1aaf9a3ad0bb1532ee3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* -*- c++ -*- */
/*
 * Copyright © 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#ifndef BRW_IR_ANALYSIS_H
#define BRW_IR_ANALYSIS_H

namespace brw {
   /**
    * Bitset of state categories that can influence the result of IR analysis
    * passes.
    */
   enum analysis_dependency_class {
      /**
       * The analysis doesn't depend on the IR, its result is effectively a
       * constant during the compilation.
       */
      DEPENDENCY_NOTHING = 0,
      /**
       * The analysis depends on the set of instructions in the program and
       * their naming.  Note that because instructions are named sequentially
       * by IP this implies a dependency on the control flow edges between
       * instructions.  This will be signaled whenever instructions are
       * inserted, removed or reordered in the program.
       */
      DEPENDENCY_INSTRUCTION_IDENTITY = 0x1,
      /**
       * The analysis is sensitive to the detailed semantics of instructions
       * in the program, where "detailed" means any change in the instruction
       * data structures other than the linked-list pointers (which are
       * already covered by DEPENDENCY_INSTRUCTION_IDENTITY).  E.g. changing
       * the negate or abs flags of an instruction source would signal this
       * flag alone because it would preserve all other instruction dependency
       * classes.
       */
      DEPENDENCY_INSTRUCTION_DETAIL = 0x2,
      /**
       * The analysis depends on the set of data flow edges between
       * instructions.  This will be signaled whenever the dataflow relation
       * between instructions has potentially changed, e.g. when the VGRF
       * index of an instruction source or destination changes (in which case
       * it will appear in combination with DEPENDENCY_INSTRUCTION_DETAIL), or
       * when data-dependent instructions are reordered (in which case it will
       * appear in combination with DEPENDENCY_INSTRUCTION_IDENTITY).
       */
      DEPENDENCY_INSTRUCTION_DATA_FLOW = 0x4,
      /**
       * The analysis depends on all instruction dependency classes.  These
       * will typically be signaled simultaneously when inserting or removing
       * instructions in the program (or if you're feeling too lazy to read
       * through your optimization pass to figure out which of the instruction
       * dependency classes above it invalidates).
       */
      DEPENDENCY_INSTRUCTIONS = 0x7,
      /**
       * The analysis depends on the set of VGRFs in the program and their
       * naming.  This will be signaled when VGRFs are allocated or released.
       */
      DEPENDENCY_VARIABLES = 0x8,
      /**
       * The analysis depends on the set of basic blocks in the program, their
       * control flow edges and naming.
       */
      DEPENDENCY_BLOCKS = 0x10,
      /**
       * The analysis depends on the program being literally the same (good
       * luck...), any change in the input invalidates previous analysis
       * computations.
       */
      DEPENDENCY_EVERYTHING = ~0
   };

   inline analysis_dependency_class
   operator|(analysis_dependency_class x, analysis_dependency_class y)
   {
      return static_cast<analysis_dependency_class>(
         static_cast<unsigned>(x) | static_cast<unsigned>(y));
   }
}

/**
 * Instantiate a program analysis class \p L which can calculate an object of
 * type \p T as result.  \p C is a closure that encapsulates whatever
 * information is required as argument to run the analysis pass.  The purpose
 * of this class is to make sure that:
 *
 *  - The analysis pass is executed lazily whenever it's needed and multiple
 *    executions are optimized out as long as the cached result remains marked
 *    up-to-date.
 *
 *  - There is no way to access the cached analysis result without first
 *    calling L::require(), which makes sure that the analysis pass is rerun
 *    if necessary.
 *
 *  - The cached result doesn't become inconsistent with the program for as
 *    long as it remains marked up-to-date. (This is only enforced in debug
 *    builds for performance reasons)
 *
 * The requirements on \p T are the following:
 *
 *  - Constructible with a single argument, as in 'x = T(c)' for \p c of type
 *    \p C.
 *
 *  - 'x.dependency_class()' on const \p x returns a bitset of
 *    brw::analysis_dependency_class specifying the set of IR objects that are
 *    required to remain invariant for the cached analysis result to be
 *    considered valid.
 *
 *  - 'x.validate(c)' on const \p x returns a boolean result specifying
 *    whether the analysis result \p x is consistent with the input IR.  This
 *    is currently only used for validation in debug builds.
 */
#define BRW_ANALYSIS(L, T, C)                                           \
   class L {                                                            \
   public:                                                              \
      /**                                                               \
       * Construct a program analysis.  \p c is an arbitrary object     \
       * passed as argument to the constructor of the analysis result   \
       * object of type \p T.                                           \
       */                                                               \
      L(C const &c) : c(c), p(NULL) {}                                  \
                                                                        \
      /**                                                               \
       * Destroy a program analysis.                                    \
       */                                                               \
      ~L()                                                              \
      {                                                                 \
         delete p;                                                      \
      }                                                                 \
                                                                        \
      /**                                                               \
       * Obtain the result of a program analysis.  This gives a         \
       * guaranteed up-to-date result, the analysis pass will be        \
       * rerun implicitly if it has become stale.                       \
       */                                                               \
      T &                                                               \
      require()                                                         \
      {                                                                 \
         if (p)                                                         \
            assert(p->validate(c));                                     \
         else                                                           \
            p = new T(c);                                               \
                                                                        \
         return *p;                                                     \
      }                                                                 \
                                                                        \
      const T &                                                         \
      require() const                                                   \
      {                                                                 \
         return const_cast<L *>(this)->require();                       \
      }                                                                 \
                                                                        \
      /**                                                               \
       * Report that dependencies of the analysis pass may have changed \
       * since the last calculation and the cached analysis result may  \
       * have to be discarded.                                          \
       */                                                               \
      void                                                              \
      invalidate(brw::analysis_dependency_class c)                      \
      {                                                                 \
         if (p && c & p->dependency_class()) {                          \
            delete p;                                                   \
            p = NULL;                                                   \
         }                                                              \
      }                                                                 \
                                                                        \
   private:                                                             \
      C c;                                                              \
      T *p;                                                             \
   }

#endif