1 // Copyright (c) 2017 The Khronos Group Inc. 2 // Copyright (c) 2017 Valve Corporation 3 // Copyright (c) 2017 LunarG Inc. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #ifndef LIBSPIRV_OPT_INSERT_EXTRACT_ELIM_PASS_H_ 18 #define LIBSPIRV_OPT_INSERT_EXTRACT_ELIM_PASS_H_ 19 20 21 #include <algorithm> 22 #include <map> 23 #include <unordered_map> 24 #include <utility> 25 26 #include "basic_block.h" 27 #include "def_use_manager.h" 28 #include "module.h" 29 #include "pass.h" 30 31 namespace spvtools { 32 namespace opt { 33 34 // See optimizer.hpp for documentation. 35 class InsertExtractElimPass : public Pass { 36 public: 37 InsertExtractElimPass(); name()38 const char* name() const override { return "insert_extract_elim"; } 39 Status Process(ir::Module*) override; 40 41 private: 42 // Return true if indices of extract |extInst| and insert |insInst| match 43 bool ExtInsMatch( 44 const ir::Instruction* extInst, const ir::Instruction* insInst) const; 45 46 // Return true if indices of extract |extInst| and insert |insInst| conflict, 47 // specifically, if the insert changes bits specified by the extract, but 48 // changes either more bits or less bits than the extract specifies, 49 // meaning the exact value being inserted cannot be used to replace 50 // the extract. 51 bool ExtInsConflict( 52 const ir::Instruction* extInst, const ir::Instruction* insInst) const; 53 54 // Look for OpExtract on sequence of OpInserts in |func|. If there is an 55 // insert with identical indices, replace the extract with the value 56 // that is inserted if possible. Specifically, replace if there is no 57 // intervening insert which conflicts. 58 bool EliminateInsertExtract(ir::Function* func); 59 60 void Initialize(ir::Module* module); 61 Pass::Status ProcessImpl(); 62 63 // Module this pass is processing 64 ir::Module* module_; 65 66 // Def-Uses for the module we are processing 67 std::unique_ptr<analysis::DefUseManager> def_use_mgr_; 68 69 // Map from function's result id to function 70 std::unordered_map<uint32_t, ir::Function*> id2function_; 71 }; 72 73 } // namespace opt 74 } // namespace spvtools 75 76 #endif // LIBSPIRV_OPT_INSERT_EXTRACT_ELIM_PASS_H_ 77 78