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
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_FUNCTION_H_
|
|
|
|
#define SOURCE_OPT_FUNCTION_H_
|
2016-05-22 18:11:24 +00:00
|
|
|
|
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>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
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
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/basic_block.h"
|
|
|
|
#include "source/opt/instruction.h"
|
|
|
|
#include "source/opt/iterator.h"
|
2016-05-22 18:11:24 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2016-05-22 18:11:24 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
// 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
|
|
|
|
2018-04-20 14:14:45 +00:00
|
|
|
// Move basic block with |id| to the position after |ip|. Both have to be
|
|
|
|
// contained in this function.
|
|
|
|
inline void MoveBasicBlockToAfter(uint32_t id, BasicBlock* ip);
|
|
|
|
|
|
|
|
// Delete all basic blocks that contain no instructions.
|
|
|
|
inline void RemoveEmptyBlocks();
|
|
|
|
|
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) {
|
2018-07-12 19:14:43 +00:00
|
|
|
return std::find_if(begin(), end(), [bb_id](const BasicBlock& it_bb) {
|
2018-03-07 18:21:11 +00:00
|
|
|
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;
|
2018-12-18 19:34:03 +00:00
|
|
|
bool WhileEachInst(const std::function<bool(Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false);
|
|
|
|
bool WhileEachInst(const std::function<bool(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;
|
2018-11-15 19:06:17 +00:00
|
|
|
void ForEachParam(const std::function<void(Instruction*)>& f,
|
|
|
|
bool run_on_debug_line_insts = false);
|
2016-05-22 18:11:24 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* InsertBasicBlockAfter(std::unique_ptr<BasicBlock>&& new_block,
|
|
|
|
BasicBlock* position);
|
2018-03-06 16:20:28 +00:00
|
|
|
|
2019-05-09 16:56:10 +00:00
|
|
|
BasicBlock* InsertBasicBlockBefore(std::unique_ptr<BasicBlock>&& new_block,
|
|
|
|
BasicBlock* position);
|
|
|
|
|
2018-11-29 19:24:58 +00:00
|
|
|
// Return true if the function calls itself either directly or indirectly.
|
|
|
|
bool IsRecursive() const;
|
|
|
|
|
SSA rewrite pass.
This pass replaces the load/store elimination passes. It implements the
SSA re-writing algorithm proposed in
Simple and Efficient Construction of Static Single Assignment Form.
Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
In: Jhala R., De Bosschere K. (eds)
Compiler Construction. CC 2013.
Lecture Notes in Computer Science, vol 7791.
Springer, Berlin, Heidelberg
https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
In contrast to common eager algorithms based on dominance and dominance
frontier information, this algorithm works backwards from load operations.
When a target variable is loaded, it queries the variable's reaching
definition. If the reaching definition is unknown at the current location,
it searches backwards in the CFG, inserting Phi instructions at join points
in the CFG along the way until it finds the desired store instruction.
The algorithm avoids repeated lookups using memoization.
For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
this algorithm is proven to produce minimal SSA. That is, it inserts the
minimal number of Phi instructions required to ensure the SSA property, but
some Phi instructions may be dead
(https://en.wikipedia.org/wiki/Static_single_assignment_form).
2018-02-22 21:18:29 +00:00
|
|
|
// Pretty-prints all the basic blocks in this function into a std::string.
|
|
|
|
//
|
|
|
|
// |options| are the disassembly options. SPV_BINARY_TO_TEXT_OPTION_NO_HEADER
|
|
|
|
// is always added to |options|.
|
|
|
|
std::string PrettyPrint(uint32_t options = 0u) const;
|
|
|
|
|
2018-09-14 17:57:12 +00:00
|
|
|
// Dump this function on stderr. Useful when running interactive
|
|
|
|
// debuggers.
|
|
|
|
void Dump() const;
|
|
|
|
|
2016-05-22 18:11:24 +00:00
|
|
|
private:
|
2016-08-09 23:20:35 +00:00
|
|
|
// 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)
|
2018-07-12 18:42:05 +00:00
|
|
|
: 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
|
|
|
}
|
|
|
|
|
2018-04-20 14:14:45 +00:00
|
|
|
inline void Function::MoveBasicBlockToAfter(uint32_t id, BasicBlock* ip) {
|
|
|
|
auto block_to_move = std::move(*FindBlock(id).Get());
|
|
|
|
|
|
|
|
assert(block_to_move->GetParent() == ip->GetParent() &&
|
|
|
|
"Both blocks have to be in the same function.");
|
|
|
|
|
|
|
|
InsertBasicBlockAfter(std::move(block_to_move), ip);
|
|
|
|
blocks_.erase(std::find(std::begin(blocks_), std::end(blocks_), nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Function::RemoveEmptyBlocks() {
|
|
|
|
auto first_empty =
|
|
|
|
std::remove_if(std::begin(blocks_), std::end(blocks_),
|
|
|
|
[](const std::unique_ptr<BasicBlock>& bb) -> bool {
|
|
|
|
return bb->GetLabelInst()->opcode() == SpvOpNop;
|
|
|
|
});
|
|
|
|
blocks_.erase(first_empty, std::end(blocks_));
|
|
|
|
}
|
|
|
|
|
2016-08-20 13:47:00 +00:00
|
|
|
inline void Function::SetFunctionEnd(std::unique_ptr<Instruction> end_inst) {
|
|
|
|
end_inst_ = std::move(end_inst);
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2016-05-22 18:11:24 +00:00
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_FUNCTION_H_
|