1 // Copyright (c) 2015-2016 The Khronos Group Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SOURCE_VAL_INSTRUCTION_H_ 16 #define SOURCE_VAL_INSTRUCTION_H_ 17 18 #include <cassert> 19 #include <cstdint> 20 #include <functional> 21 #include <utility> 22 #include <vector> 23 24 #include "source/table.h" 25 #include "spirv-tools/libspirv.h" 26 27 namespace spvtools { 28 namespace val { 29 30 class BasicBlock; 31 class Function; 32 33 /// Wraps the spv_parsed_instruction struct along with use and definition of the 34 /// instruction's result id 35 class Instruction { 36 public: 37 explicit Instruction(const spv_parsed_instruction_t* inst); 38 39 /// Registers the use of the Instruction in instruction \p inst at \p index 40 void RegisterUse(const Instruction* inst, uint32_t index); 41 42 uint32_t id() const { return inst_.result_id; } 43 uint32_t type_id() const { return inst_.type_id; } 44 SpvOp opcode() const { return static_cast<SpvOp>(inst_.opcode); } 45 46 /// Returns the Function where the instruction was defined. nullptr if it was 47 /// defined outside of a Function 48 const Function* function() const { return function_; } 49 void set_function(Function* func) { function_ = func; } 50 51 /// Returns the BasicBlock where the instruction was defined. nullptr if it 52 /// was defined outside of a BasicBlock 53 const BasicBlock* block() const { return block_; } 54 void set_block(BasicBlock* b) { block_ = b; } 55 56 /// Returns a vector of pairs of all references to this instruction's result 57 /// id. The first element is the instruction in which this result id was 58 /// referenced and the second is the index of the word in that instruction 59 /// where this result id appeared 60 const std::vector<std::pair<const Instruction*, uint32_t>>& uses() const { 61 return uses_; 62 } 63 64 /// The word used to define the Instruction 65 uint32_t word(size_t index) const { return words_[index]; } 66 67 /// The words used to define the Instruction 68 const std::vector<uint32_t>& words() const { return words_; } 69 70 /// Returns the operand at |idx|. 71 const spv_parsed_operand_t& operand(size_t idx) const { 72 return operands_[idx]; 73 } 74 75 /// The operands of the Instruction 76 const std::vector<spv_parsed_operand_t>& operands() const { 77 return operands_; 78 } 79 80 /// Provides direct access to the stored C instruction object. 81 const spv_parsed_instruction_t& c_inst() const { return inst_; } 82 83 /// Provides direct access to instructions spv_ext_inst_type_t object. 84 const spv_ext_inst_type_t& ext_inst_type() const { 85 return inst_.ext_inst_type; 86 } 87 88 // Casts the words belonging to the operand under |index| to |T| and returns. 89 template <typename T> 90 T GetOperandAs(size_t index) const { 91 const spv_parsed_operand_t& o = operands_.at(index); 92 assert(o.num_words * 4 >= sizeof(T)); 93 assert(o.offset + o.num_words <= inst_.num_words); 94 return *reinterpret_cast<const T*>(&words_[o.offset]); 95 } 96 97 size_t LineNum() const { return line_num_; } 98 void SetLineNum(size_t pos) { line_num_ = pos; } 99 100 private: 101 const std::vector<uint32_t> words_; 102 const std::vector<spv_parsed_operand_t> operands_; 103 spv_parsed_instruction_t inst_; 104 size_t line_num_ = 0; 105 106 /// The function in which this instruction was declared 107 Function* function_ = nullptr; 108 109 /// The basic block in which this instruction was declared 110 BasicBlock* block_ = nullptr; 111 112 /// This is a vector of pairs of all references to this instruction's result 113 /// id. The first element is the instruction in which this result id was 114 /// referenced and the second is the index of the word in the referencing 115 /// instruction where this instruction appeared 116 std::vector<std::pair<const Instruction*, uint32_t>> uses_; 117 }; 118 119 bool operator<(const Instruction& lhs, const Instruction& rhs); 120 bool operator<(const Instruction& lhs, uint32_t rhs); 121 bool operator==(const Instruction& lhs, const Instruction& rhs); 122 bool operator==(const Instruction& lhs, uint32_t rhs); 123 124 } // namespace val 125 } // namespace spvtools 126 127 // custom specialization of std::hash for Instruction 128 namespace std { 129 template <> 130 struct hash<spvtools::val::Instruction> { 131 typedef spvtools::val::Instruction argument_type; 132 typedef std::size_t result_type; 133 result_type operator()(const argument_type& inst) const { 134 return hash<uint32_t>()(inst.id()); 135 } 136 }; 137 138 } // namespace std 139 140 #endif // SOURCE_VAL_INSTRUCTION_H_ 141