diff --git a/source/opt/basic_block.h b/source/opt/basic_block.h index 360eaefc4..62b50ceed 100644 --- a/source/opt/basic_block.h +++ b/source/opt/basic_block.h @@ -36,6 +36,7 @@ #include #include "instruction.h" +#include "iterator.h" namespace spvtools { namespace ir { @@ -45,16 +46,22 @@ class Function; // A SPIR-V basic block. class BasicBlock { public: - // Creates a basic block with the given enclosing |function| and starting - // |label|. - BasicBlock(std::unique_ptr label) - : function_(nullptr), label_(std::move(label)) {} + using iterator = UptrVectorIterator; + using const_iterator = UptrVectorIterator; + + // Creates a basic block with the given starting |label|. + inline explicit BasicBlock(std::unique_ptr 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 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& f); @@ -71,6 +78,9 @@ class BasicBlock { std::vector> insts_; }; +inline BasicBlock::BasicBlock(std::unique_ptr label) + : function_(nullptr), label_(std::move(label)) {} + inline void BasicBlock::AddInstruction(std::unique_ptr i) { insts_.emplace_back(std::move(i)); } diff --git a/source/opt/function.h b/source/opt/function.h index 70c7ff132..8615a3672 100644 --- a/source/opt/function.h +++ b/source/opt/function.h @@ -47,11 +47,9 @@ class Function { using iterator = UptrVectorIterator; using const_iterator = UptrVectorIterator; - // Creates a function instance declared by the given instruction |def_inst|. - Function(std::unique_ptr 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 def_inst); // Sets the enclosing module for this function. void SetParent(Module* module) { module_ = module; } @@ -59,6 +57,7 @@ class Function { inline void AddParameter(std::unique_ptr p); // Appends a basic block to this function. inline void AddBasicBlock(std::unique_ptr b); + iterator begin() { return iterator(&blocks_, blocks_.begin()); } iterator end() { return iterator(&blocks_, blocks_.end()); } const_iterator cbegin() { return const_iterator(&blocks_, blocks_.cbegin()); } @@ -84,6 +83,11 @@ class Function { Instruction end_inst_; }; +inline Function::Function(std::unique_ptr def_inst) + : module_(nullptr), + def_inst_(std::move(def_inst)), + end_inst_(SpvOpFunctionEnd) {} + inline void Function::AddParameter(std::unique_ptr p) { params_.emplace_back(std::move(p)); } diff --git a/source/opt/module.cpp b/source/opt/module.cpp index d83d21ef6..c08b2fd5d 100644 --- a/source/opt/module.cpp +++ b/source/opt/module.cpp @@ -57,6 +57,15 @@ std::vector Module::GetConstants() { return insts; }; +std::vector Module::GetConstants() const { + std::vector 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& f) { for (auto& i : capabilities_) f(i.get()); for (auto& i : extensions_) f(i.get()); diff --git a/source/opt/module.h b/source/opt/module.h index ac93acfc6..f34c9117b 100644 --- a/source/opt/module.h +++ b/source/opt/module.h @@ -94,6 +94,7 @@ class Module { // Returns a vector of pointers to constant-creation instructions in this // module. std::vector GetConstants(); + std::vector GetConstants() const; // Iterators for debug instructions (excluding OpLine & OpNoLine) contained in // this module.