]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Add pg_assume(expr) macro
authorAndres Freund <andres@anarazel.de>
Wed, 9 Jul 2025 22:38:05 +0000 (18:38 -0400)
committerAndres Freund <andres@anarazel.de>
Wed, 9 Jul 2025 22:38:05 +0000 (18:38 -0400)
This macro can be used to avoid compiler warnings, particularly when using -O3
and not using assertions, and to get the compiler to generate better code.

A subsequent commit introduces a first user.

Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/3prdb6hkep3duglhsujrn52bkvnlkvhc54fzvph2emrsm4vodl@77yy6j4hkemb
Discussion: https://postgr.es/m/20230316172818.x6375uvheom3ibt2%40awork3.anarazel.de
Discussion: https://postgr.es/m/20240207203138.sknifhlppdtgtxnk%40awork3.anarazel.de

src/include/c.h

index 04fd23577dee7c246365e9c216e06df5ef8fb4b5..6d4495bdd9f68733d0fa262bc6937781a61a9e85 100644 (file)
 #define pg_unreachable() abort()
 #endif
 
+/*
+ * pg_assume(expr) states that we assume `expr` to evaluate to true. In assert
+ * enabled builds pg_assume() is turned into an assertion, in optimized builds
+ * we try to clue the compiler into the fact that `expr` is true.
+ *
+ * This is useful for two purposes:
+ *
+ * 1) Avoid compiler warnings by telling the compiler about assumptions the
+ *       code makes. This is particularly useful when building with optimizations
+ *       and w/o assertions.
+ *
+ * 2) Help the compiler to generate more efficient code
+ *
+ * It is unspecified whether `expr` is evaluated, therefore it better be
+ * side-effect free.
+ */
+#if defined(USE_ASSERT_CHECKING)
+#define pg_assume(expr) Assert(expr)
+#elif defined(HAVE__BUILTIN_UNREACHABLE)
+#define pg_assume(expr) \
+       do { \
+               if (!(expr)) \
+                       __builtin_unreachable(); \
+       } while (0)
+#elif defined(_MSC_VER)
+#define pg_assume(expr) __assume(expr)
+#else
+#define pg_assume(expr) ((void) 0)
+#endif
+
 /*
  * Hints to the compiler about the likelihood of a branch. Both likely() and
  * unlikely() return the boolean value of the contained expression.