2016-11-11 17:04:14 +00:00
|
|
|
/*
|
2021-01-14 15:07:49 +00:00
|
|
|
* Copyright 2016-2021 Arm Limited
|
2021-05-08 08:47:48 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
2016-11-11 17:04:14 +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
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-11-25 14:22:08 +00:00
|
|
|
/*
|
|
|
|
* At your option, you may choose to accept this material under either:
|
|
|
|
* 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
|
|
|
|
* 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
|
|
|
|
*/
|
|
|
|
|
2016-11-11 17:04:14 +00:00
|
|
|
#ifndef SPIRV_CROSS_CFG_HPP
|
|
|
|
#define SPIRV_CROSS_CFG_HPP
|
|
|
|
|
2017-03-25 15:25:30 +00:00
|
|
|
#include "spirv_common.hpp"
|
2017-03-25 15:28:44 +00:00
|
|
|
#include <assert.h>
|
2016-11-11 17:04:14 +00:00
|
|
|
|
2019-03-29 09:29:44 +00:00
|
|
|
namespace SPIRV_CROSS_NAMESPACE
|
2016-11-11 17:04:14 +00:00
|
|
|
{
|
2017-03-25 15:25:30 +00:00
|
|
|
class Compiler;
|
2016-11-11 17:04:14 +00:00
|
|
|
class CFG
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CFG(Compiler &compiler, const SPIRFunction &function);
|
|
|
|
|
|
|
|
Compiler &get_compiler()
|
|
|
|
{
|
|
|
|
return compiler;
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:45:11 +00:00
|
|
|
const Compiler &get_compiler() const
|
|
|
|
{
|
|
|
|
return compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SPIRFunction &get_function() const
|
|
|
|
{
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
2016-11-11 17:04:14 +00:00
|
|
|
uint32_t get_immediate_dominator(uint32_t block) const
|
|
|
|
{
|
2019-01-10 13:35:34 +00:00
|
|
|
auto itr = immediate_dominators.find(block);
|
|
|
|
if (itr != std::end(immediate_dominators))
|
|
|
|
return itr->second;
|
|
|
|
else
|
|
|
|
return 0;
|
2016-11-11 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 15:45:11 +00:00
|
|
|
uint32_t get_visit_order(uint32_t block) const
|
2016-11-11 17:04:14 +00:00
|
|
|
{
|
2019-01-10 13:35:34 +00:00
|
|
|
auto itr = visit_order.find(block);
|
|
|
|
assert(itr != std::end(visit_order));
|
|
|
|
int v = itr->second.get();
|
2016-11-18 15:45:11 +00:00
|
|
|
assert(v > 0);
|
|
|
|
return uint32_t(v);
|
2016-11-11 17:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t find_common_dominator(uint32_t a, uint32_t b) const;
|
|
|
|
|
2019-04-02 09:19:03 +00:00
|
|
|
const SmallVector<uint32_t> &get_preceding_edges(uint32_t block) const
|
2016-12-15 16:14:47 +00:00
|
|
|
{
|
2019-01-10 13:35:34 +00:00
|
|
|
auto itr = preceding_edges.find(block);
|
|
|
|
if (itr != std::end(preceding_edges))
|
|
|
|
return itr->second;
|
|
|
|
else
|
|
|
|
return empty_vector;
|
2016-12-15 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:19:03 +00:00
|
|
|
const SmallVector<uint32_t> &get_succeeding_edges(uint32_t block) const
|
2016-12-15 16:14:47 +00:00
|
|
|
{
|
2019-01-10 13:35:34 +00:00
|
|
|
auto itr = succeeding_edges.find(block);
|
|
|
|
if (itr != std::end(succeeding_edges))
|
|
|
|
return itr->second;
|
|
|
|
else
|
|
|
|
return empty_vector;
|
2016-12-15 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Op>
|
2017-07-25 16:22:15 +00:00
|
|
|
void walk_from(std::unordered_set<uint32_t> &seen_blocks, uint32_t block, const Op &op) const
|
2016-12-15 16:14:47 +00:00
|
|
|
{
|
2017-07-25 16:22:15 +00:00
|
|
|
if (seen_blocks.count(block))
|
|
|
|
return;
|
|
|
|
seen_blocks.insert(block);
|
|
|
|
|
2019-08-26 13:36:23 +00:00
|
|
|
if (op(block))
|
|
|
|
{
|
|
|
|
for (auto b : get_succeeding_edges(block))
|
|
|
|
walk_from(seen_blocks, b, op);
|
|
|
|
}
|
2016-12-15 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 09:10:39 +00:00
|
|
|
uint32_t find_loop_dominator(uint32_t block) const;
|
|
|
|
|
2019-09-05 10:43:40 +00:00
|
|
|
bool node_terminates_control_flow_in_sub_graph(BlockID from, BlockID to) const;
|
2019-08-26 13:36:23 +00:00
|
|
|
|
2016-11-11 17:04:14 +00:00
|
|
|
private:
|
2019-01-10 13:35:34 +00:00
|
|
|
struct VisitOrder
|
|
|
|
{
|
|
|
|
int &get()
|
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int &get() const
|
|
|
|
{
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
int v = -1;
|
|
|
|
};
|
|
|
|
|
2016-11-11 17:04:14 +00:00
|
|
|
Compiler &compiler;
|
|
|
|
const SPIRFunction &func;
|
2019-04-02 09:19:03 +00:00
|
|
|
std::unordered_map<uint32_t, SmallVector<uint32_t>> preceding_edges;
|
|
|
|
std::unordered_map<uint32_t, SmallVector<uint32_t>> succeeding_edges;
|
2019-01-10 13:35:34 +00:00
|
|
|
std::unordered_map<uint32_t, uint32_t> immediate_dominators;
|
|
|
|
std::unordered_map<uint32_t, VisitOrder> visit_order;
|
2019-04-02 09:19:03 +00:00
|
|
|
SmallVector<uint32_t> post_order;
|
|
|
|
SmallVector<uint32_t> empty_vector;
|
2016-11-11 17:04:14 +00:00
|
|
|
|
|
|
|
void add_branch(uint32_t from, uint32_t to);
|
|
|
|
void build_post_order_visit_order();
|
|
|
|
void build_immediate_dominators();
|
2016-11-17 21:15:07 +00:00
|
|
|
bool post_order_visit(uint32_t block);
|
2016-11-11 17:04:14 +00:00
|
|
|
uint32_t visit_count = 0;
|
|
|
|
|
2016-11-18 08:59:54 +00:00
|
|
|
bool is_back_edge(uint32_t to) const;
|
2019-07-03 09:18:50 +00:00
|
|
|
bool has_visited_forward_edge(uint32_t to) const;
|
2016-11-11 17:04:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DominatorBuilder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DominatorBuilder(const CFG &cfg);
|
|
|
|
|
|
|
|
void add_block(uint32_t block);
|
|
|
|
uint32_t get_dominator() const
|
|
|
|
{
|
|
|
|
return dominator;
|
|
|
|
}
|
|
|
|
|
2016-11-18 15:45:11 +00:00
|
|
|
void lift_continue_block_dominator();
|
|
|
|
|
2016-11-11 17:04:14 +00:00
|
|
|
private:
|
|
|
|
const CFG &cfg;
|
|
|
|
uint32_t dominator = 0;
|
|
|
|
};
|
2019-04-02 09:19:03 +00:00
|
|
|
} // namespace SPIRV_CROSS_NAMESPACE
|
2016-11-11 17:04:14 +00:00
|
|
|
|
|
|
|
#endif
|