diff options
Diffstat (limited to 'src/shader_recompiler/frontend/ir/post_order.cpp')
| -rw-r--r-- | src/shader_recompiler/frontend/ir/post_order.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/shader_recompiler/frontend/ir/post_order.cpp b/src/shader_recompiler/frontend/ir/post_order.cpp new file mode 100644 index 000000000..16bc44101 --- /dev/null +++ b/src/shader_recompiler/frontend/ir/post_order.cpp | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | // Copyright 2021 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | |||
| 7 | #include <boost/container/flat_set.hpp> | ||
| 8 | #include <boost/container/small_vector.hpp> | ||
| 9 | |||
| 10 | #include "shader_recompiler/frontend/ir/basic_block.h" | ||
| 11 | #include "shader_recompiler/frontend/ir/post_order.h" | ||
| 12 | |||
| 13 | namespace Shader::IR { | ||
| 14 | |||
| 15 | BlockList PostOrder(const AbstractSyntaxNode& root) { | ||
| 16 | boost::container::small_vector<Block*, 16> block_stack; | ||
| 17 | boost::container::flat_set<Block*> visited; | ||
| 18 | BlockList post_order_blocks; | ||
| 19 | |||
| 20 | if (root.type != AbstractSyntaxNode::Type::Block) { | ||
| 21 | throw LogicError("First node in abstract syntax list root is not a block"); | ||
| 22 | } | ||
| 23 | Block* const first_block{root.data.block}; | ||
| 24 | visited.insert(first_block); | ||
| 25 | block_stack.push_back(first_block); | ||
| 26 | |||
| 27 | while (!block_stack.empty()) { | ||
| 28 | Block* const block{block_stack.back()}; | ||
| 29 | const auto visit{[&](Block* branch) { | ||
| 30 | if (!visited.insert(branch).second) { | ||
| 31 | return false; | ||
| 32 | } | ||
| 33 | // Calling push_back twice is faster than insert on MSVC | ||
| 34 | block_stack.push_back(block); | ||
| 35 | block_stack.push_back(branch); | ||
| 36 | return true; | ||
| 37 | }}; | ||
| 38 | block_stack.pop_back(); | ||
| 39 | if (std::ranges::none_of(block->ImmSuccessors(), visit)) { | ||
| 40 | post_order_blocks.push_back(block); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | return post_order_blocks; | ||
| 44 | } | ||
| 45 | |||
| 46 | } // namespace Shader::IR | ||