1 // Copyright (c) 2022 The Khronos Group Inc. 2 // Copyright (c) 2022 LunarG Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #ifndef SOURCE_OPT_LIVENESS_H_ 17 #define SOURCE_OPT_LIVENESS_H_ 18 19 #include <cstdint> 20 #include <unordered_set> 21 22 namespace spvtools { 23 namespace opt { 24 25 class IRContext; 26 class Instruction; 27 28 namespace analysis { 29 30 class Type; 31 32 // This class represents the liveness of the input variables of a module 33 class LivenessManager { 34 public: 35 LivenessManager(IRContext* ctx); 36 37 // Copy liveness info into |live_locs| and |builtin_locs|. 38 void GetLiveness(std::unordered_set<uint32_t>* live_locs, 39 std::unordered_set<uint32_t>* live_builtins); 40 41 // Return true if builtin |bi| is being analyzed. 42 bool IsAnalyzedBuiltin(uint32_t bi); 43 44 // Determine starting loc |offset| and the type |cur_type| of 45 // access chain |ac|. Set |no_loc| to true if no loc found. 46 // |is_patch| indicates if patch variable. |input| is true 47 // if input variable, otherwise output variable. 48 void AnalyzeAccessChainLoc(const Instruction* ac, 49 const analysis::Type** curr_type, uint32_t* offset, 50 bool* no_loc, bool is_patch, bool input = true); 51 52 // Return size of |type_id| in units of locations 53 uint32_t GetLocSize(const analysis::Type* type) const; 54 55 private: context()56 IRContext* context() const { return ctx_; } 57 58 // Initialize analysis 59 void InitializeAnalysis(); 60 61 // Analyze |id| for builtin var and struct members. Return true if builtins 62 // found. 63 bool AnalyzeBuiltIn(uint32_t id); 64 65 // Mark all live locations resulting from |user| of |var| at |loc|. 66 void MarkRefLive(const Instruction* user, Instruction* var); 67 68 // Mark |count| locations starting at location |start|. 69 void MarkLocsLive(uint32_t start, uint32_t count); 70 71 // Return type of component of aggregate type |agg_type| at |index| 72 const analysis::Type* GetComponentType(uint32_t index, 73 const analysis::Type* agg_type) const; 74 75 // Return offset of |index| into aggregate type |agg_type| in units of 76 // input locations 77 uint32_t GetLocOffset(uint32_t index, const analysis::Type* agg_type) const; 78 79 // Populate live_locs_ and live_builtins_ 80 void ComputeLiveness(); 81 82 // IR context that owns this liveness manager. 83 IRContext* ctx_; 84 85 // True if live_locs_ and live_builtins_ are computed 86 bool computed_; 87 88 // Live locations 89 std::unordered_set<uint32_t> live_locs_; 90 91 // Live builtins 92 std::unordered_set<uint32_t> live_builtins_; 93 }; 94 95 } // namespace analysis 96 } // namespace opt 97 } // namespace spvtools 98 99 #endif // SOURCE_OPT_LIVENESS_H_ 100