2016-05-22 18:11:24 +00:00
|
|
|
// Copyright (c) 2016 Google 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-05-22 18:11:24 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-05-22 18:11:24 +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-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
#ifndef LIBSPIRV_OPT_CONSTRUCTS_H_
|
|
|
|
#define LIBSPIRV_OPT_CONSTRUCTS_H_
|
|
|
|
|
2018-03-07 18:21:11 +00:00
|
|
|
#include <algorithm>
|
2016-05-22 18:11:24 +00:00
|
|
|
#include <functional>
|
2016-08-09 23:20:35 +00:00
|
|
|
#include <memory>
|
2016-05-22 18:11:24 +00:00
|
|
|
#include <utility>
|
2016-08-09 23:20:35 +00:00
|
|
|
#include <vector>
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
#include "basic_block.h"
|
|
|
|
#include "instruction.h"
|
2016-08-10 14:02:28 +00:00
|
|
|
#include "iterator.h"
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace ir {
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
class CFG;
|
2017-11-14 19:11:50 +00:00
|
|
|
class IRContext;
|
2016-05-22 18:11:24 +00:00
|
|
|
class Module;
|
|
|
|
|
|
|
|
// A SPIR-V function.
|
|
|
|
class Function {
|
|
|
|
public:
|
2016-08-10 14:02:28 +00:00
|
|
|
using iterator = UptrVectorIterator<BasicBlock>;
|
|
|
|
using const_iterator = UptrVectorIterator<BasicBlock, true>;
|
|
|
|
|
2016-08-12 13:13:04 +00:00
|
|
|
// Creates a function instance declared by the given OpFunction instruction
|
|
|
|
// |def_inst|.
|
|
|
|
inline explicit Function(std::unique_ptr<Instruction> def_inst);
|
2017-11-14 19:11:50 +00:00
|
|
|
|
|
|
|
explicit Function(const Function& f) = delete;
|
|
|
|
|
|
|
|
// Creates a clone of the instruction in the given |context|
|
2017-07-13 00:16:51 +00:00
|
|
|
//
|
|
|
|
// The parent module will default to null and needs to be explicitly set by
|
|
|
|
// the user.
|
2017-11-14 19:11:50 +00:00
|
|
|
Function* Clone(IRContext*) const;
|
2017-03-26 19:08:41 +00:00
|
|
|
// The OpFunction instruction that begins the definition of this function.
|
|
|
|
Instruction& DefInst() { return *def_inst_; }
|
2017-07-13 00:16:51 +00:00
|
|
|
const Instruction& DefInst() const { return *def_inst_; }
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
// Sets the enclosing module for this function.
|
|
|
|
void SetParent(Module* module) { module_ = module; }
|
2017-11-14 19:11:50 +00:00
|
|
|
// Gets the enclosing module for this function
|
|
|
|
Module* GetParent() const { return module_; }
|
2016-05-22 18:11:24 +00:00
|
|
|
// Appends a parameter to this function.
|
2016-08-09 23:20:35 +00:00
|
|
|
inline void AddParameter(std::unique_ptr<Instruction> p);
|
2016-05-22 18:11:24 +00:00
|
|
|
// Appends a basic block to this function.
|
2016-08-09 23:20:35 +00:00
|
|
|
inline void AddBasicBlock(std::unique_ptr<BasicBlock> b);
|
2018-02-12 21:42:15 +00:00
|
|
|
// Appends a basic block to this function at the position |ip|.
|
|
|
|
inline void AddBasicBlock(std::unique_ptr<BasicBlock> b, iterator ip);
|
|
|
|
template <typename T>
|
|
|
|
inline void AddBasicBlocks(T begin, T end, iterator ip);
|
2016-08-12 13:13:04 +00:00
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
// Saves the given function end instruction.
|
|
|
|
inline void SetFunctionEnd(std::unique_ptr<Instruction> end_inst);
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
// Returns the given function end instruction.
|
2018-01-03 00:54:55 +00:00
|
|
|
inline Instruction* EndInst() { return end_inst_.get(); }
|
|
|
|
inline const Instruction* EndInst() const { return end_inst_.get(); }
|
2017-07-13 00:16:51 +00:00
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
// Returns function's id
|
|
|
|
inline uint32_t result_id() const { return def_inst_->result_id(); }
|
|
|
|
|
2017-07-13 00:16:51 +00:00
|
|
|
// Returns function's return type id
|
2016-11-10 17:11:50 +00:00
|
|
|
inline uint32_t type_id() const { return def_inst_->type_id(); }
|
|
|
|
|
2017-09-06 12:56:41 +00:00
|
|
|
// Returns the entry basic block for this function.
|
|
|
|
const std::unique_ptr<BasicBlock>& entry() const { return blocks_.front(); }
|
|
|
|
|
2016-08-10 14:02:28 +00:00
|
|
|
iterator begin() { return iterator(&blocks_, blocks_.begin()); }
|
|
|
|
iterator end() { return iterator(&blocks_, blocks_.end()); }
|
2017-11-27 21:21:26 +00:00
|
|
|
const_iterator begin() const { return cbegin(); }
|
|
|
|
const_iterator end() const { return cend(); }
|
2017-03-31 14:36:10 +00:00
|
|
|
const_iterator cbegin() const {
|
|
|
|
return const_iterator(&blocks_, blocks_.cbegin());
|
|
|
|
}
|
|
|
|
const_iterator cend() const {
|
|
|
|
return const_iterator(&blocks_, blocks_.cend());
|
|
|
|
}
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2018-03-07 18:21:11 +00:00
|
|
|
// Returns an iterator to the basic block |id|.
|
|
|
|
iterator FindBlock(uint32_t bb_id) {
|
|
|
|
return std::find_if(begin(), end(), [bb_id](const ir::BasicBlock& it_bb) {
|
|
|
|
return bb_id == it_bb.id();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
// Runs the given function |f| on each instruction in this function, and
|
|
|
|
// optionally on debug line instructions that might precede them.
|
|
|
|
void ForEachInst(const std::function<void(Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false);
|
|
|
|
void ForEachInst(const std::function<void(const Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false) const;
|
2016-11-10 17:11:50 +00:00
|
|
|
|
|
|
|
// Runs the given function |f| on each parameter instruction in this function,
|
|
|
|
// and optionally on debug line instructions that might precede them.
|
|
|
|
void ForEachParam(const std::function<void(const Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false) const;
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
// Returns the context of the current function.
|
|
|
|
IRContext* context() const { return def_inst_->context(); }
|
|
|
|
|
|
|
|
BasicBlock* InsertBasicBlockAfter(std::unique_ptr<ir::BasicBlock>&& new_block,
|
|
|
|
BasicBlock* position);
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
private:
|
2016-08-09 23:20:35 +00:00
|
|
|
// The enclosing module.
|
|
|
|
Module* module_;
|
|
|
|
// The OpFunction instruction that begins the definition of this function.
|
|
|
|
std::unique_ptr<Instruction> def_inst_;
|
|
|
|
// All parameters to this function.
|
|
|
|
std::vector<std::unique_ptr<Instruction>> params_;
|
2017-05-05 02:55:53 +00:00
|
|
|
// All basic blocks inside this function in specification order
|
2016-08-09 23:20:35 +00:00
|
|
|
std::vector<std::unique_ptr<BasicBlock>> blocks_;
|
|
|
|
// The OpFunctionEnd instruction.
|
2016-08-20 13:47:00 +00:00
|
|
|
std::unique_ptr<Instruction> end_inst_;
|
2016-05-22 18:11:24 +00:00
|
|
|
};
|
|
|
|
|
2018-01-10 19:23:47 +00:00
|
|
|
// Pretty-prints |func| to |str|. Returns |str|.
|
|
|
|
std::ostream& operator<<(std::ostream& str, const Function& func);
|
|
|
|
|
2016-08-12 13:13:04 +00:00
|
|
|
inline Function::Function(std::unique_ptr<Instruction> def_inst)
|
2016-08-20 13:47:00 +00:00
|
|
|
: module_(nullptr), def_inst_(std::move(def_inst)), end_inst_() {}
|
2016-08-12 13:13:04 +00:00
|
|
|
|
2016-08-09 23:20:35 +00:00
|
|
|
inline void Function::AddParameter(std::unique_ptr<Instruction> p) {
|
|
|
|
params_.emplace_back(std::move(p));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Function::AddBasicBlock(std::unique_ptr<BasicBlock> b) {
|
2018-02-12 21:42:15 +00:00
|
|
|
AddBasicBlock(std::move(b), end());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Function::AddBasicBlock(std::unique_ptr<BasicBlock> b,
|
|
|
|
iterator ip) {
|
|
|
|
ip.InsertBefore(std::move(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void Function::AddBasicBlocks(T src_begin, T src_end, iterator ip) {
|
|
|
|
blocks_.insert(ip.Get(), std::make_move_iterator(src_begin),
|
|
|
|
std::make_move_iterator(src_end));
|
2016-08-09 23:20:35 +00:00
|
|
|
}
|
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
inline void Function::SetFunctionEnd(std::unique_ptr<Instruction> end_inst) {
|
|
|
|
end_inst_ = std::move(end_inst);
|
|
|
|
}
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
} // namespace ir
|
|
|
|
} // namespace spvtools
|
|
|
|
|
|
|
|
#endif // LIBSPIRV_OPT_CONSTRUCTS_H_
|