FreeTensor
Loading...
Searching...
No Matches
all_uses.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_ALL_USES_H
2#define FREE_TENSOR_ALL_USES_H
3
4#include <unordered_set>
5
6#include <analyze/find_stmt.h>
7#include <visitor.h>
8
9namespace freetensor {
10
14class AllUses : public Visitor {
15 public:
16 typedef int AllUsesType;
17 static constexpr AllUsesType CHECK_LOAD = 0x1;
18 static constexpr AllUsesType CHECK_STORE = 0x2;
19 static constexpr AllUsesType CHECK_REDUCE = 0x4;
20 static constexpr AllUsesType CHECK_VAR = 0x8;
21
22 private:
23 AllUsesType type_;
24 bool noRecurseIdx_, noRecurseSubStmt_;
25 bool inFirstStmt_ = false;
26 std::unordered_set<std::string> uses_;
27
28 public:
29 AllUses(AllUsesType type, bool noRecurseIdx, bool noRecurseSubStmt)
30 : type_(type), noRecurseIdx_(noRecurseIdx),
31 noRecurseSubStmt_(noRecurseSubStmt) {}
32
33 const std::unordered_set<std::string> &uses() const { return uses_; }
34
35 protected:
36 // TODO: Do we need to check viewOf, reduction, Alloc or Free?
37 void visitStmt(const Stmt &s) override;
38 void visit(const Load &op) override;
39 void visit(const Store &op) override;
40 void visit(const ReduceTo &op) override;
41 void visit(const Var &op) override;
42};
43
58std::unordered_set<std::string>
59allUses(const AST &op,
62 bool noRecurseIdx = false, bool noRecurseSubStmt = false);
63inline std::unordered_set<std::string>
64allUses(const auto &filter, const Stmt &op,
67 bool noRecurseIdx = false) {
68 std::unordered_set<std::string> ret;
69 for (auto &&stmt : findAllStmt(op, filter)) {
70 for (auto &&use : allUses(stmt, type, noRecurseIdx, true)) {
71 ret.emplace(use);
72 }
73 }
74 return ret;
75}
83inline std::unordered_set<std::string> allReads(const AST &op,
84 bool noRecurseIdx = false,
85 bool noRecurseSubStmt = false) {
86 return allUses(op, AllUses::CHECK_LOAD, noRecurseIdx, noRecurseSubStmt);
87}
88inline std::unordered_set<std::string>
89allReads(const auto &filter, const Stmt &op, bool noRecurseIdx = false) {
90 return allUses(filter, op, AllUses::CHECK_LOAD, noRecurseIdx);
91}
99inline std::unordered_set<std::string>
100allWrites(const AST &op, bool noRecurseIdx = false,
101 bool noRecurseSubStmt = false) {
103 noRecurseIdx, noRecurseSubStmt);
104}
105inline std::unordered_set<std::string>
106allWrites(const auto &filter, const Stmt &op, bool noRecurseIdx = false) {
108 noRecurseIdx);
109}
117inline std::unordered_set<std::string> allIters(const AST &op,
118 bool noRecurseIdx = false,
119 bool noRecurseSubStmt = false) {
120 return allUses(op, AllUses::CHECK_VAR, noRecurseIdx, noRecurseSubStmt);
121}
122inline std::unordered_set<std::string>
123allIters(const auto &filter, const Stmt &op, bool noRecurseIdx = false) {
124 return allUses(filter, op, AllUses::CHECK_VAR, noRecurseIdx);
125}
134inline std::unordered_set<std::string> allNames(const AST &op,
135 bool noRecurseIdx = false,
136 bool noRecurseSubStmt = false) {
137 return allUses(op,
140 noRecurseIdx, noRecurseSubStmt);
141}
142inline std::unordered_set<std::string>
143allNames(const auto &filter, const Stmt &op, bool noRecurseIdx = false) {
144 return allUses(filter, op,
147 noRecurseIdx);
148}
151} // namespace freetensor
152
153#endif // FREE_TENSOR_ALL_USES_H
Definition: all_uses.h:14
static constexpr AllUsesType CHECK_REDUCE
Definition: all_uses.h:19
void visitStmt(const Stmt &s) override
Definition: all_uses.cc:5
AllUses(AllUsesType type, bool noRecurseIdx, bool noRecurseSubStmt)
Definition: all_uses.h:29
const std::unordered_set< std::string > & uses() const
Definition: all_uses.h:33
static constexpr AllUsesType CHECK_STORE
Definition: all_uses.h:18
int AllUsesType
Definition: all_uses.h:16
static constexpr AllUsesType CHECK_VAR
Definition: all_uses.h:20
void visit(const Load &op) override
Definition: all_uses.cc:14
static constexpr AllUsesType CHECK_LOAD
Definition: all_uses.h:17
Definition: visitor.h:11
Definition: allocator.h:9
std::unordered_set< std::string > allIters(const AST &op, bool noRecurseIdx=false, bool noRecurseSubStmt=false)
Definition: all_uses.h:117
std::unordered_set< std::string > allWrites(const AST &op, bool noRecurseIdx=false, bool noRecurseSubStmt=false)
Definition: all_uses.h:100
std::vector< T > filter(const std::vector< T > &vec, const U &callback)
Definition: container_utils.h:131
Ref< ASTNode > AST
Definition: ast.h:149
std::unordered_set< std::string > allUses(const AST &op, AllUses::AllUsesType type=AllUses::CHECK_LOAD|AllUses::CHECK_STORE|AllUses::CHECK_REDUCE, bool noRecurseIdx=false, bool noRecurseSubStmt=false)
Definition: all_uses.cc:52
std::unordered_set< std::string > allReads(const AST &op, bool noRecurseIdx=false, bool noRecurseSubStmt=false)
Definition: all_uses.h:83
std::unordered_set< std::string > allNames(const AST &op, bool noRecurseIdx=false, bool noRecurseSubStmt=false)
Definition: all_uses.h:134
std::vector< Stmt > findAllStmt(const Stmt &ast, const ID &id)
Definition: find_stmt.cc:32