[llvm] [Support] Do not use `llvm::size` in `getLoopPreheader` (PR #94540)

Ben Barham via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 5 15:44:29 PDT 2024


https://github.com/bnbarham created https://github.com/llvm/llvm-project/pull/94540

`BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader()` was changed in 7243607867393a2b8ccd477e95e6f62d00f3206f to use `llvm::size` rather than the checking that `child_begin() + 1 == child_end()`. `llvm::size` requires that `std::distance` be O(1) and hence that clients support random access. Switch back to the original check.

>From 527f574e479cd5810b47a6861a128a55ac2c27c0 Mon Sep 17 00:00:00 2001
From: Ben Barham <ben_barham at apple.com>
Date: Sun, 28 Apr 2024 18:55:09 -0700
Subject: [PATCH] [Support] Do not use `llvm::size` in `getLoopPreheader`

`BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader()` was changed in
7243607867393a2b8ccd477e95e6f62d00f3206f to use `llvm::size` rather than
the checking that `child_begin() + 1 == child_end()`. `llvm::size`
requires that `std::distance` be O(1) and hence that clients support
random access. Switch back to the original check.
---
 llvm/include/llvm/Support/GenericLoopInfoImpl.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/GenericLoopInfoImpl.h b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
index 1e0d0ee446fc4..c9c7354937545 100644
--- a/llvm/include/llvm/Support/GenericLoopInfoImpl.h
+++ b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
@@ -208,7 +208,10 @@ BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
     return nullptr;
 
   // Make sure there is only one exit out of the preheader.
-  if (llvm::size(llvm::children<BlockT *>(Out)) != 1)
+  typedef GraphTraits<BlockT *> BlockTraits;
+  typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
+  ++SI;
+  if (SI != BlockTraits::child_end(Out))
     return nullptr; // Multiple exits from the block, must not be a preheader.
 
   // The predecessor has exactly one successor, so it is a preheader.



More information about the llvm-commits mailing list