#include <vector>
#include "instruction.h"
+#include "iterator.h"
namespace spvtools {
namespace ir {
// A SPIR-V basic block.
class BasicBlock {
public:
- // Creates a basic block with the given enclosing |function| and starting
- // |label|.
- BasicBlock(std::unique_ptr<Instruction> label)
- : function_(nullptr), label_(std::move(label)) {}
+ using iterator = UptrVectorIterator<Instruction>;
+ using const_iterator = UptrVectorIterator<Instruction, true>;
+
+ // Creates a basic block with the given starting |label|.
+ inline explicit BasicBlock(std::unique_ptr<Instruction> label);
// Sets the enclosing function for this basic block.
void SetParent(Function* function) { function_ = function; }
// Appends an instruction to this basic block.
inline void AddInstruction(std::unique_ptr<Instruction> i);
+ iterator begin() { return iterator(&insts_, insts_.begin()); }
+ iterator end() { return iterator(&insts_, insts_.end()); }
+ const_iterator cbegin() { return const_iterator(&insts_, insts_.cbegin()); }
+ const_iterator cend() { return const_iterator(&insts_, insts_.cend()); }
+
// Runs the given function |f| on each instruction in this basic block.
inline void ForEachInst(const std::function<void(Instruction*)>& f);
std::vector<std::unique_ptr<Instruction>> insts_;
};
+inline BasicBlock::BasicBlock(std::unique_ptr<Instruction> label)
+ : function_(nullptr), label_(std::move(label)) {}
+
inline void BasicBlock::AddInstruction(std::unique_ptr<Instruction> i) {
insts_.emplace_back(std::move(i));
}
using iterator = UptrVectorIterator<BasicBlock>;
using const_iterator = UptrVectorIterator<BasicBlock, true>;
- // Creates a function instance declared by the given instruction |def_inst|.
- Function(std::unique_ptr<Instruction> def_inst)
- : module_(nullptr),
- def_inst_(std::move(def_inst)),
- end_inst_(SpvOpFunctionEnd) {}
+ // Creates a function instance declared by the given OpFunction instruction
+ // |def_inst|.
+ inline explicit Function(std::unique_ptr<Instruction> def_inst);
// Sets the enclosing module for this function.
void SetParent(Module* module) { module_ = module; }
inline void AddParameter(std::unique_ptr<Instruction> p);
// Appends a basic block to this function.
inline void AddBasicBlock(std::unique_ptr<BasicBlock> b);
+
iterator begin() { return iterator(&blocks_, blocks_.begin()); }
iterator end() { return iterator(&blocks_, blocks_.end()); }
const_iterator cbegin() { return const_iterator(&blocks_, blocks_.cbegin()); }
Instruction end_inst_;
};
+inline Function::Function(std::unique_ptr<Instruction> def_inst)
+ : module_(nullptr),
+ def_inst_(std::move(def_inst)),
+ end_inst_(SpvOpFunctionEnd) {}
+
inline void Function::AddParameter(std::unique_ptr<Instruction> p) {
params_.emplace_back(std::move(p));
}
return insts;
};
+std::vector<const Instruction*> Module::GetConstants() const {
+ std::vector<const Instruction*> insts;
+ for (uint32_t i = 0; i < types_values_.size(); ++i) {
+ if (IsConstantInst(types_values_[i]->opcode()))
+ insts.push_back(types_values_[i].get());
+ }
+ return insts;
+};
+
void Module::ForEachInst(const std::function<void(Instruction*)>& f) {
for (auto& i : capabilities_) f(i.get());
for (auto& i : extensions_) f(i.get());
// Returns a vector of pointers to constant-creation instructions in this
// module.
std::vector<Instruction*> GetConstants();
+ std::vector<const Instruction*> GetConstants() const;
// Iterators for debug instructions (excluding OpLine & OpNoLine) contained in
// this module.