VapourSynth-llvmexpr
Loading...
Searching...
No Matches
VarInitPass.cpp
Go to the documentation of this file.
1
19
20#include "VarInitPass.hpp"
24#include "BlockAnalysisPass.hpp"
25
26#include <algorithm>
27#include <iterator>
28#include <set>
29
30namespace analysis {
31
32class VarInitAnalysis : public ForwardDataflowAnalysis<std::set<std::string>> {
33 const std::set<std::string>& all_vars;
34
35 public:
36 explicit VarInitAnalysis(const std::set<std::string>& all_vars)
37 : all_vars(all_vars) {}
38
39 std::set<std::string>
40 computeGenSet(size_t block_idx, const std::vector<Token>& tokens,
41 const std::vector<CFGBlock>& cfg_blocks) override {
42 std::set<std::string> gen_set;
43 const auto& block = cfg_blocks[block_idx];
44 for (int j = block.start_token_idx; j < block.end_token_idx; ++j) {
45 if (tokens[j].type == TokenType::VarStore) {
46 gen_set.insert(
47 std::get<TokenPayloadVar>(tokens[j].payload).name);
48 } else if (tokens[j].type == TokenType::ArrayAllocStatic ||
49 tokens[j].type == TokenType::ArrayAllocDyn) {
50 const auto& array_name =
51 std::get<TokenPayloadArrayOp>(tokens[j].payload).name;
52 gen_set.insert(var_naming::getArrayName(array_name));
53 }
54 }
55 return gen_set;
56 }
57
58 std::set<std::string>
59 meetOperation(const std::vector<std::set<std::string>>& inputs) override {
60 if (inputs.empty()) {
61 return {};
62 }
63
64 std::set<std::string> result = inputs[0];
65 for (size_t i = 1; i < inputs.size(); ++i) {
66 std::set<std::string> temp_intersect;
67 std::set_intersection(
68 result.begin(), result.end(), inputs[i].begin(),
69 inputs[i].end(),
70 std::inserter(temp_intersect, temp_intersect.begin()));
71 result = std::move(temp_intersect);
72 }
73 return result;
74 }
75
76 std::set<std::string>
77 transferFunction(const std::set<std::string>& in_value,
78 const std::set<std::string>& gen_set) override {
79 std::set<std::string> out_value = gen_set;
80 out_value.insert(in_value.begin(), in_value.end());
81 return out_value;
82 }
83
84 std::set<std::string> getBoundaryValue() override { return {}; }
85
86 std::set<std::string> getInitialOutValue() override { return all_vars; }
87};
88
89VarInitPass::Result VarInitPass::run(const std::vector<Token>& tokens,
90 AnalysisManager& am) {
91 const auto& block_result = am.getResult<BlockAnalysisPass>();
92 const auto& cfg_blocks = block_result.cfg_blocks;
93
94 // Collect all variables and arrays
95 std::set<std::string> all_vars;
96 for (const auto& token : tokens) {
97 if (token.type == TokenType::VarStore ||
98 token.type == TokenType::VarLoad) {
99 all_vars.insert(std::get<TokenPayloadVar>(token.payload).name);
100 } else if (token.type == TokenType::ArrayAllocStatic ||
101 token.type == TokenType::ArrayAllocDyn ||
102 token.type == TokenType::ArrayStore ||
103 token.type == TokenType::ArrayLoad) {
104 const auto& array_name =
105 std::get<TokenPayloadArrayOp>(token.payload).name;
106 all_vars.insert(var_naming::getArrayName(array_name));
107 }
108 }
109
110 VarInitAnalysis analysis(all_vars);
111 auto [in_sets, out_sets] = analysis.analyze(tokens, cfg_blocks);
112
113 Result result;
114 result.var_init_in = std::move(in_sets);
115 result.var_init_out = std::move(out_sets);
116
117 return result;
118}
119
120} // namespace analysis
@ ArrayAllocStatic
Definition Tokenizer.hpp:51
std::set< std::string > meetOperation(const std::vector< std::set< std::string > > &inputs) override
std::set< std::string > transferFunction(const std::set< std::string > &in_value, const std::set< std::string > &gen_set) override
std::set< std::string > computeGenSet(size_t block_idx, const std::vector< Token > &tokens, const std::vector< CFGBlock > &cfg_blocks) override
std::set< std::string > getInitialOutValue() override
VarInitAnalysis(const std::set< std::string > &all_vars)
std::set< std::string > getBoundaryValue() override
Result run(const std::vector< Token > &tokens, AnalysisManager &am) override
std::string getArrayName(std::string_view base_name)
Definition VarNaming.hpp:30
std::vector< std::set< std::string > > var_init_out
std::vector< std::set< std::string > > var_init_in