yuzu/src/shader_recompiler/frontend/ir/post_order.cpp

47 lines
1.4 KiB
C++
Raw Normal View History

2021-02-15 00:15:42 +01:00
// Copyright 2021 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
2021-02-15 00:15:42 +01:00
#include <boost/container/flat_set.hpp>
#include <boost/container/small_vector.hpp>
#include "shader_recompiler/frontend/ir/basic_block.h"
#include "shader_recompiler/frontend/ir/post_order.h"
namespace Shader::IR {
BlockList PostOrder(const AbstractSyntaxNode& root) {
2021-02-15 00:15:42 +01:00
boost::container::small_vector<Block*, 16> block_stack;
boost::container::flat_set<Block*> visited;
BlockList post_order_blocks;
if (root.type != AbstractSyntaxNode::Type::Block) {
throw LogicError("First node in abstract syntax list root is not a block");
}
Block* const first_block{root.block};
2021-02-15 00:15:42 +01:00
visited.insert(first_block);
block_stack.push_back(first_block);
while (!block_stack.empty()) {
Block* const block{block_stack.back()};
const auto visit{[&](Block* branch) {
if (!visited.insert(branch).second) {
return false;
}
// Calling push_back twice is faster than insert on MSVC
block_stack.push_back(block);
block_stack.push_back(branch);
return true;
}};
2021-02-15 00:15:42 +01:00
block_stack.pop_back();
if (std::ranges::none_of(block->ImmSuccessors(), visit)) {
2021-02-15 00:15:42 +01:00
post_order_blocks.push_back(block);
}
}
return post_order_blocks;
}
} // namespace Shader::IR