[PATCH] D136261: Templates for singleton loop queries

Michael Kruse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 19 23:29:56 PDT 2022


Meinersbur added a comment.

Thank you for the patch.

However, I think it can be improved even more. It is sufficient to just have a single `Result` variable but continue iterating through the list. When a second is found, return nullptr. If looking for unique items, check that the second item is actually different before returning nullptr.

This may have less code-reuse but doesn't need additional templates and is more efficient without a `SmallVector`. There will never be a non-stack allocation needed for, but I don't think a compiler can statically derive that. You could even create generic ranges-like helpers in `STLExtras.h` for this kind of algorithm and reuse is in `RegionBase::getEnteringBlock()` etc.

If you don't know what I mean, we can commit this patch first, then I could create a followup-one for you to review.



================
Comment at: llvm/include/llvm/Analysis/LoopInfoImpl.h:98-103
   assert(!isInvalid() && "Loop not in a valid state!");
-  SmallVector<BlockT *, 8> ExitBlocks;
-  getExitBlocks(ExitBlocks);
+  SmallVector<BlockT *, 2> ExitBlocks;
+  getExitBlocks</*StopAfterFindingTwo*/ true>(ExitBlocks);
   if (ExitBlocks.size() == 1)
     return ExitBlocks[0];
   return nullptr;
----------------



================
Comment at: llvm/include/llvm/Analysis/LoopInfoImpl.h:166-170
+  SmallVector<BlockT *, 2> UniqueExitBlocks;
+  getUniqueExitBlocks</*StopAfterFindingTwo*/ true>(UniqueExitBlocks);
   if (UniqueExitBlocks.size() == 1)
     return UniqueExitBlocks[0];
   return nullptr;
----------------



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D136261/new/

https://reviews.llvm.org/D136261



More information about the llvm-commits mailing list