1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | #ifndef GLOW_GRAPH_USEDEF_H |
17 | #define GLOW_GRAPH_USEDEF_H |
18 | |
19 | #include <list> |
20 | |
21 | namespace glow { |
22 | |
23 | /// A UseDef is something that can be an operand for an instruction. |
24 | template <typename UserTy, typename Use> class UseDef { |
25 | /// A list of users. Notice that the same user may appear twice in the list. |
26 | /// This is typically a very short list. |
27 | std::list<Use> users_{}; |
28 | |
29 | public: |
30 | UseDef() = default; |
31 | |
32 | /// Removes the use \p U from the uselist. |
33 | void removeUse(Use U) { |
34 | auto it = std::find(users_.begin(), users_.end(), U); |
35 | assert(it != users_.end() && "User not in list" ); |
36 | users_.erase(it); |
37 | } |
38 | /// Adds the use \p U. |
39 | void addUse(Use U) { users_.push_back(U); } |
40 | |
41 | /// \returns True if the value has some users. |
42 | bool hasUsers() const { return !users_.empty(); } |
43 | |
44 | /// \returns true if there is a single use to this value. |
45 | bool hasOneUse() const { return users_.size() == 1; } |
46 | |
47 | /// \returns the number of users that the value has. |
48 | unsigned getNumUsers() const { return users_.size(); } |
49 | |
50 | /// Returns true if the user \p I is in the list. |
51 | bool hasUser(const UserTy *I) const { |
52 | for (const auto &U : users_) { |
53 | if (U.getUser() == I) { |
54 | return true; |
55 | } |
56 | } |
57 | return false; |
58 | } |
59 | |
60 | /// \returns the list of users for this value. |
61 | std::list<Use> &getUsers() { return users_; } |
62 | |
63 | /// \returns the list of users for this value. |
64 | const std::list<Use> &getUsers() const { return users_; } |
65 | }; |
66 | |
67 | } // namespace glow |
68 | |
69 | #endif // GLOW_GRAPH_USEDEF_H |
70 | |