Skip to content

Commit 79a7ed9

Browse files
committed
CFGDiff: Simplify/common the begin/end implementations to use a common range helper
(would be nice to revisit the CFG traits and change them to use ranges rather than begin/end - if anyone wants to do that refactor) Also use more auto because writing the names of range utilty iterators isn't helping readability here - they're sort of implementation details for the most part, especially once you nest a few different filtering and adapting iterators.
1 parent 59918d3 commit 79a7ed9

File tree

1 file changed

+29
-45
lines changed

1 file changed

+29
-45
lines changed

llvm/include/llvm/IR/CFGDiff.h

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -158,58 +158,42 @@ template <typename GraphT, bool InverseGraph = false, bool InverseEdge = false,
158158
typename GT = GraphTraits<GraphT>>
159159
struct CFGViewChildren {
160160
using DataRef = const GraphDiff<typename GT::NodeRef, InverseGraph> *;
161-
using RawNodeRef = typename GT::NodeRef;
162-
using NodeRef = std::pair<DataRef, RawNodeRef>;
163-
164-
using ExistingChildIterator =
165-
WrappedPairNodeDataIterator<typename GT::ChildIteratorType, NodeRef,
166-
DataRef>;
167-
struct DeletedEdgesFilter {
168-
RawNodeRef BB;
169-
DeletedEdgesFilter(RawNodeRef BB) : BB(BB){};
170-
bool operator()(NodeRef N) const {
171-
return !N.first->ignoreChild(BB, N.second, InverseEdge);
172-
}
173-
};
174-
using FilterExistingChildrenIterator =
175-
filter_iterator<ExistingChildIterator, DeletedEdgesFilter>;
176-
177-
using vec_iterator = typename SmallVectorImpl<RawNodeRef>::const_iterator;
178-
using AddNewChildrenIterator =
179-
WrappedPairNodeDataIterator<vec_iterator, NodeRef, DataRef>;
180-
using ChildIteratorType =
181-
concat_iterator<NodeRef, FilterExistingChildrenIterator,
182-
AddNewChildrenIterator>;
183-
184-
static ChildIteratorType child_begin(NodeRef N) {
185-
auto InsertVec = N.first->getAddedChildren(N.second, InverseEdge);
186-
// filter iterator init:
187-
auto firstit = make_filter_range(
188-
make_range<ExistingChildIterator>({GT::child_begin(N.second), N.first},
189-
{GT::child_end(N.second), N.first}),
190-
DeletedEdgesFilter(N.second));
191-
// new inserts iterator init:
192-
auto secondit = make_range<AddNewChildrenIterator>(
193-
{InsertVec.begin(), N.first}, {InsertVec.end(), N.first});
161+
using NodeRef = std::pair<DataRef, typename GT::NodeRef>;
194162

195-
return concat_iterator<NodeRef, FilterExistingChildrenIterator,
196-
AddNewChildrenIterator>(firstit, secondit);
163+
template<typename Range>
164+
static auto makeChildRange(Range &&R, DataRef DR) {
165+
using Iter = WrappedPairNodeDataIterator<decltype(std::forward<Range>(R).begin()), NodeRef, DataRef>;
166+
return make_range(Iter(R.begin(), DR), Iter(R.end(), DR));
197167
}
198168

199-
static ChildIteratorType child_end(NodeRef N) {
200-
auto InsertVec = N.first->getAddedChildren(N.second, InverseEdge);
169+
static auto children(NodeRef N) {
170+
201171
// filter iterator init:
202-
auto firstit = make_filter_range(
203-
make_range<ExistingChildIterator>({GT::child_end(N.second), N.first},
204-
{GT::child_end(N.second), N.first}),
205-
DeletedEdgesFilter(N.second));
172+
auto R = make_range(GT::child_begin(N.second), GT::child_end(N.second));
173+
auto First = make_filter_range(makeChildRange(R, N.first), [&](NodeRef C) {
174+
return !C.first->ignoreChild(N.second, C.second, InverseEdge);
175+
});
176+
206177
// new inserts iterator init:
207-
auto secondit = make_range<AddNewChildrenIterator>(
208-
{InsertVec.end(), N.first}, {InsertVec.end(), N.first});
178+
auto InsertVec = N.first->getAddedChildren(N.second, InverseEdge);
179+
auto Second = makeChildRange(InsertVec, N.first);
180+
181+
auto CR = concat<NodeRef>(First, Second);
182+
// concat_range contains references to other ranges, returning it would
183+
// leave those references dangling - the iterators contain
184+
// other iterators by value so they're safe to return.
185+
return make_range(CR.begin(), CR.end());
186+
}
209187

210-
return concat_iterator<NodeRef, FilterExistingChildrenIterator,
211-
AddNewChildrenIterator>(firstit, secondit);
188+
static auto child_begin(NodeRef N) {
189+
return children(N).begin();
212190
}
191+
192+
static auto child_end(NodeRef N) {
193+
return children(N).end();
194+
}
195+
196+
using ChildIteratorType = decltype(child_end(std::declval<NodeRef>()));
213197
};
214198

215199
template <typename T, bool B>

0 commit comments

Comments
 (0)