2016-08-06 17:29:33 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-08-06 17:29:33 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-06 17:29:33 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2016-08-06 17:29:33 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_VAL_INSTRUCTION_H_
|
|
|
|
#define SOURCE_VAL_INSTRUCTION_H_
|
2016-08-06 17:29:33 +00:00
|
|
|
|
2017-05-04 17:35:52 +00:00
|
|
|
#include <cassert>
|
2016-08-06 17:29:33 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-12-18 23:10:29 +00:00
|
|
|
#include "source/ext_inst.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/table.h"
|
2016-08-06 17:29:33 +00:00
|
|
|
#include "spirv-tools/libspirv.h"
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
namespace spvtools {
|
2018-07-10 03:18:44 +00:00
|
|
|
namespace val {
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
class BasicBlock;
|
|
|
|
class Function;
|
|
|
|
|
|
|
|
/// Wraps the spv_parsed_instruction struct along with use and definition of the
|
|
|
|
/// instruction's result id
|
|
|
|
class Instruction {
|
|
|
|
public:
|
2018-08-02 19:12:06 +00:00
|
|
|
explicit Instruction(const spv_parsed_instruction_t* inst);
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
/// Registers the use of the Instruction in instruction \p inst at \p index
|
|
|
|
void RegisterUse(const Instruction* inst, uint32_t index);
|
|
|
|
|
|
|
|
uint32_t id() const { return inst_.result_id; }
|
|
|
|
uint32_t type_id() const { return inst_.type_id; }
|
|
|
|
SpvOp opcode() const { return static_cast<SpvOp>(inst_.opcode); }
|
|
|
|
|
|
|
|
/// Returns the Function where the instruction was defined. nullptr if it was
|
|
|
|
/// defined outside of a Function
|
|
|
|
const Function* function() const { return function_; }
|
2018-08-02 19:12:06 +00:00
|
|
|
void set_function(Function* func) { function_ = func; }
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
/// Returns the BasicBlock where the instruction was defined. nullptr if it
|
|
|
|
/// was defined outside of a BasicBlock
|
|
|
|
const BasicBlock* block() const { return block_; }
|
2018-08-02 19:12:06 +00:00
|
|
|
void set_block(BasicBlock* b) { block_ = b; }
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
/// Returns a vector of pairs of all references to this instruction's result
|
|
|
|
/// id. The first element is the instruction in which this result id was
|
|
|
|
/// referenced and the second is the index of the word in that instruction
|
|
|
|
/// where this result id appeared
|
|
|
|
const std::vector<std::pair<const Instruction*, uint32_t>>& uses() const {
|
|
|
|
return uses_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The word used to define the Instruction
|
2016-08-09 18:05:03 +00:00
|
|
|
uint32_t word(size_t index) const { return words_[index]; }
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
/// The words used to define the Instruction
|
|
|
|
const std::vector<uint32_t>& words() const { return words_; }
|
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
/// Returns the operand at |idx|.
|
|
|
|
const spv_parsed_operand_t& operand(size_t idx) const {
|
|
|
|
return operands_[idx];
|
|
|
|
}
|
|
|
|
|
2016-08-06 17:29:33 +00:00
|
|
|
/// The operands of the Instruction
|
|
|
|
const std::vector<spv_parsed_operand_t>& operands() const {
|
|
|
|
return operands_;
|
|
|
|
}
|
|
|
|
|
2017-05-03 19:26:39 +00:00
|
|
|
/// Provides direct access to the stored C instruction object.
|
2017-11-08 17:40:02 +00:00
|
|
|
const spv_parsed_instruction_t& c_inst() const { return inst_; }
|
2017-05-03 19:26:39 +00:00
|
|
|
|
2018-07-10 14:57:52 +00:00
|
|
|
/// Provides direct access to instructions spv_ext_inst_type_t object.
|
|
|
|
const spv_ext_inst_type_t& ext_inst_type() const {
|
|
|
|
return inst_.ext_inst_type;
|
|
|
|
}
|
|
|
|
|
2019-12-18 23:10:29 +00:00
|
|
|
bool IsNonSemantic() const {
|
|
|
|
return opcode() == SpvOp::SpvOpExtInst &&
|
|
|
|
spvExtInstIsNonSemantic(inst_.ext_inst_type);
|
|
|
|
}
|
|
|
|
|
2020-01-23 22:04:30 +00:00
|
|
|
/// True if this is an OpExtInst for debug info extension.
|
|
|
|
bool IsDebugInfo() const {
|
|
|
|
return opcode() == SpvOp::SpvOpExtInst &&
|
|
|
|
spvExtInstIsDebugInfo(inst_.ext_inst_type);
|
|
|
|
}
|
|
|
|
|
2017-05-04 17:35:52 +00:00
|
|
|
// Casts the words belonging to the operand under |index| to |T| and returns.
|
|
|
|
template <typename T>
|
|
|
|
T GetOperandAs(size_t index) const {
|
2018-07-10 14:57:52 +00:00
|
|
|
const spv_parsed_operand_t& o = operands_.at(index);
|
|
|
|
assert(o.num_words * 4 >= sizeof(T));
|
|
|
|
assert(o.offset + o.num_words <= inst_.num_words);
|
|
|
|
return *reinterpret_cast<const T*>(&words_[o.offset]);
|
2017-05-04 17:35:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 20:12:07 +00:00
|
|
|
size_t LineNum() const { return line_num_; }
|
|
|
|
void SetLineNum(size_t pos) { line_num_ = pos; }
|
2018-06-19 20:02:44 +00:00
|
|
|
|
2016-08-06 17:29:33 +00:00
|
|
|
private:
|
|
|
|
const std::vector<uint32_t> words_;
|
|
|
|
const std::vector<spv_parsed_operand_t> operands_;
|
|
|
|
spv_parsed_instruction_t inst_;
|
2018-08-02 19:12:06 +00:00
|
|
|
size_t line_num_ = 0;
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
/// The function in which this instruction was declared
|
2018-08-02 19:12:06 +00:00
|
|
|
Function* function_ = nullptr;
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
/// The basic block in which this instruction was declared
|
2018-08-02 19:12:06 +00:00
|
|
|
BasicBlock* block_ = nullptr;
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
/// This is a vector of pairs of all references to this instruction's result
|
|
|
|
/// id. The first element is the instruction in which this result id was
|
|
|
|
/// referenced and the second is the index of the word in the referencing
|
|
|
|
/// instruction where this instruction appeared
|
|
|
|
std::vector<std::pair<const Instruction*, uint32_t>> uses_;
|
|
|
|
};
|
|
|
|
|
2018-08-02 19:12:06 +00:00
|
|
|
bool operator<(const Instruction& lhs, const Instruction& rhs);
|
|
|
|
bool operator<(const Instruction& lhs, uint32_t rhs);
|
|
|
|
bool operator==(const Instruction& lhs, const Instruction& rhs);
|
|
|
|
bool operator==(const Instruction& lhs, uint32_t rhs);
|
2016-08-06 17:29:33 +00:00
|
|
|
|
2018-07-10 03:18:44 +00:00
|
|
|
} // namespace val
|
2018-07-07 13:38:00 +00:00
|
|
|
} // namespace spvtools
|
2016-08-06 17:29:33 +00:00
|
|
|
|
|
|
|
// custom specialization of std::hash for Instruction
|
|
|
|
namespace std {
|
|
|
|
template <>
|
2018-07-10 03:18:44 +00:00
|
|
|
struct hash<spvtools::val::Instruction> {
|
|
|
|
typedef spvtools::val::Instruction argument_type;
|
2016-08-06 17:29:33 +00:00
|
|
|
typedef std::size_t result_type;
|
|
|
|
result_type operator()(const argument_type& inst) const {
|
|
|
|
return hash<uint32_t>()(inst.id());
|
|
|
|
}
|
|
|
|
};
|
2018-07-10 03:18:44 +00:00
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
} // namespace std
|
2016-08-06 17:29:33 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_VAL_INSTRUCTION_H_
|