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 elk { 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 * elk::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 elk_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 */ elk_analysis(const C * c)140 elk_analysis(const C *c) : c(c), p(NULL) {} 141 elk_analysis(const elk_analysis &) = delete; 142 143 /** 144 * Destroy a program analysis. 145 */ ~elk_analysis()146 ~elk_analysis() 147 { 148 delete p; 149 } 150 151 elk_analysis & operator=(const elk_analysis &) = delete; 152 153 /** 154 * Obtain the result of a program analysis. This gives a 155 * guaranteed up-to-date result, the analysis pass will be 156 * rerun implicitly if it has become stale. 157 */ 158 T & require()159 require() 160 { 161 if (p) 162 assert(p->validate(c)); 163 else 164 p = new T(c); 165 166 return *p; 167 } 168 169 const T & require()170 require() const 171 { 172 return const_cast<elk_analysis<T, C> *>(this)->require(); 173 } 174 175 /** 176 * Report that dependencies of the analysis pass may have changed 177 * since the last calculation and the cached analysis result may 178 * have to be discarded. 179 */ 180 void invalidate(elk::analysis_dependency_class c)181 invalidate(elk::analysis_dependency_class c) 182 { 183 if (p && (c & p->dependency_class())) { 184 delete p; 185 p = NULL; 186 } 187 } 188 189 private: 190 const C *c; 191 T *p; 192 }; 193