[PATCH] D45792: [ADT] Teach reverse() about filter_iterator ranges

Vedant Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 18 19:07:35 PDT 2018


vsk created this revision.
vsk added reviewers: timshen, zturner, fhahn, dblaikie, mattd, aprantl.
Herald added a subscriber: JDevlieghere.

This makes it possible to reverse a filtered range. For example, here's
a way to visit memory accesses in a BasicBlock in reverse order:

  auto Stores = reverse(make_filter_range(BB, [](Instruction &I) {
    return isa<StoreInst>(&I) || isa<LoadInst>(&I);
  }));
  
  for (auto *SI : Stores)
    ...

To implement this functionality, I added an overload of reverse() which
is enabled just for filter_iterator ranges. The overload invalidates the
original range and creates a new filter_iterator range using reverse
iterators.

I'd appreciate any comments about how to make this cleaner. The only
alternative approach I considered was to get filter_iterator to adopt
bidirectional_iterator_tag and define an operator-- conditionally, but I
couldn't get this to work out.

This was motivated by a problem we encountered in https://reviews.llvm.org/D45657: we'd like to
visit the non-debug-info instructions in a BasicBlock in reverse order.

Testing: check-llvm, check-clang


https://reviews.llvm.org/D45792

Files:
  include/llvm/ADT/STLExtras.h
  unittests/ADT/IteratorTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45792.143030.patch
Type: text/x-patch
Size: 7108 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180419/3b50a903/attachment.bin>


More information about the llvm-commits mailing list