[llvm] [Support] Use "if constexpr" in shouldReverseIterate (PR #156812)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 4 09:15:26 PDT 2025


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/156812

>From 3985d8f4428d5b7bd372f111174b15ca9c084156 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 3 Sep 2025 10:54:26 -0700
Subject: [PATCH 1/2] [Support] Use "if constexpr" in shouldReverseIterate

This patch simplifies shouldReverseIterate with "if constexpr" while
switching to "constexpr bool", allowing compile-time evaluation at
call sites.
---
 llvm/include/llvm/Support/ReverseIteration.h | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/Support/ReverseIteration.h b/llvm/include/llvm/Support/ReverseIteration.h
index 9e9411856369e..456d423600d6e 100644
--- a/llvm/include/llvm/Support/ReverseIteration.h
+++ b/llvm/include/llvm/Support/ReverseIteration.h
@@ -6,13 +6,11 @@
 
 namespace llvm {
 
-template<class T = void *>
-bool shouldReverseIterate() {
-#if LLVM_ENABLE_REVERSE_ITERATION
-  return detail::IsPointerLike<T>::value;
-#else
-  return false;
-#endif
+template <class T = void *> constexpr bool shouldReverseIterate() {
+  if constexpr (LLVM_ENABLE_REVERSE_ITERATION)
+    return detail::IsPointerLike<T>::value;
+  else
+    return false;
 }
 
 } // namespace llvm

>From 41a94f2ecbaebffc16fab7e98549e3b26eeb4676 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 4 Sep 2025 09:09:18 -0700
Subject: [PATCH 2/2] Address a comment.

---
 llvm/include/llvm/Support/ReverseIteration.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm/Support/ReverseIteration.h b/llvm/include/llvm/Support/ReverseIteration.h
index 456d423600d6e..1a42ff3368dcb 100644
--- a/llvm/include/llvm/Support/ReverseIteration.h
+++ b/llvm/include/llvm/Support/ReverseIteration.h
@@ -7,10 +7,11 @@
 namespace llvm {
 
 template <class T = void *> constexpr bool shouldReverseIterate() {
-  if constexpr (LLVM_ENABLE_REVERSE_ITERATION)
-    return detail::IsPointerLike<T>::value;
-  else
-    return false;
+#if LLVM_ENABLE_REVERSE_ITERATION
+  return detail::IsPointerLike<T>::value;
+#else
+  return false;
+#endif
 }
 
 } // namespace llvm



More information about the llvm-commits mailing list