2016-11-11 20:04:14 +03:00
|
|
|
/*
|
2018-02-01 01:08:43 +03:00
|
|
|
* Copyright 2016-2018 ARM Limited
|
2016-11-11 20:04:14 +03: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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SPIRV_CROSS_CFG_HPP
|
|
|
|
#define SPIRV_CROSS_CFG_HPP
|
|
|
|
|
2017-03-25 18:25:30 +03:00
|
|
|
#include "spirv_common.hpp"
|
2017-03-25 18:28:44 +03:00
|
|
|
#include <assert.h>
|
2016-11-11 20:04:14 +03:00
|
|
|
|
|
|
|
namespace spirv_cross
|
|
|
|
{
|
2017-03-25 18:25:30 +03:00
|
|
|
class Compiler;
|
2016-11-11 20:04:14 +03:00
|
|
|
class CFG
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CFG(Compiler &compiler, const SPIRFunction &function);
|
|
|
|
|
|
|
|
Compiler &get_compiler()
|
|
|
|
{
|
|
|
|
return compiler;
|
|
|
|
}
|
|
|
|
|
2016-11-18 18:45:11 +03:00
|
|
|
const Compiler &get_compiler() const
|
|
|
|
{
|
|
|
|
return compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SPIRFunction &get_function() const
|
|
|
|
{
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2016-11-11 20:04:14 +03:00
|
|
|
uint32_t get_immediate_dominator(uint32_t block) const
|
|
|
|
{
|
|
|
|
return immediate_dominators[block];
|
|
|
|
}
|
|
|
|
|
2016-11-18 18:45:11 +03:00
|
|
|
uint32_t get_visit_order(uint32_t block) const
|
2016-11-11 20:04:14 +03:00
|
|
|
{
|
2016-11-18 18:45:11 +03:00
|
|
|
int v = visit_order[block];
|
|
|
|
assert(v > 0);
|
|
|
|
return uint32_t(v);
|
2016-11-11 20:04:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t find_common_dominator(uint32_t a, uint32_t b) const;
|
|
|
|
|
2016-12-15 19:14:47 +03:00
|
|
|
const std::vector<uint32_t> &get_preceding_edges(uint32_t block) const
|
|
|
|
{
|
|
|
|
return preceding_edges[block];
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<uint32_t> &get_succeeding_edges(uint32_t block) const
|
|
|
|
{
|
|
|
|
return succeeding_edges[block];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Op>
|
2017-07-25 19:22:15 +03:00
|
|
|
void walk_from(std::unordered_set<uint32_t> &seen_blocks, uint32_t block, const Op &op) const
|
2016-12-15 19:14:47 +03:00
|
|
|
{
|
2017-07-25 19:22:15 +03:00
|
|
|
if (seen_blocks.count(block))
|
|
|
|
return;
|
|
|
|
seen_blocks.insert(block);
|
|
|
|
|
2016-12-15 19:14:47 +03:00
|
|
|
op(block);
|
|
|
|
for (auto b : succeeding_edges[block])
|
2017-07-25 19:22:15 +03:00
|
|
|
walk_from(seen_blocks, b, op);
|
2016-12-15 19:14:47 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 20:04:14 +03:00
|
|
|
private:
|
|
|
|
Compiler &compiler;
|
|
|
|
const SPIRFunction &func;
|
|
|
|
std::vector<std::vector<uint32_t>> preceding_edges;
|
|
|
|
std::vector<std::vector<uint32_t>> succeeding_edges;
|
|
|
|
std::vector<uint32_t> immediate_dominators;
|
|
|
|
std::vector<int> visit_order;
|
|
|
|
std::vector<uint32_t> post_order;
|
|
|
|
|
|
|
|
void add_branch(uint32_t from, uint32_t to);
|
|
|
|
void build_post_order_visit_order();
|
|
|
|
void build_immediate_dominators();
|
2016-11-18 00:15:07 +03:00
|
|
|
bool post_order_visit(uint32_t block);
|
2016-11-11 20:04:14 +03:00
|
|
|
uint32_t visit_count = 0;
|
|
|
|
|
2016-11-18 11:59:54 +03:00
|
|
|
bool is_back_edge(uint32_t to) const;
|
2016-11-11 20:04:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
class DominatorBuilder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DominatorBuilder(const CFG &cfg);
|
|
|
|
|
|
|
|
void add_block(uint32_t block);
|
|
|
|
uint32_t get_dominator() const
|
|
|
|
{
|
|
|
|
return dominator;
|
|
|
|
}
|
|
|
|
|
2016-11-18 18:45:11 +03:00
|
|
|
void lift_continue_block_dominator();
|
|
|
|
|
2016-11-11 20:04:14 +03:00
|
|
|
private:
|
|
|
|
const CFG &cfg;
|
|
|
|
uint32_t dominator = 0;
|
|
|
|
};
|
2018-04-03 15:08:15 +03:00
|
|
|
} // namespace spirv_cross
|
2016-11-11 20:04:14 +03:00
|
|
|
|
|
|
|
#endif
|