[libcxx-commits] [PATCH] D73732: Add _LIBCPP_BUILTIN_CONSTANT_P support.

Martijn Vels via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jan 30 11:25:07 PST 2020


mvels created this revision.
Herald added subscribers: libcxx-commits, christof.
Herald added a project: libc++.
mvels edited the summary of this revision.
mvels added a reviewer: EricWF.
Herald added a reviewer: mclow.lists.

This change adds the macros _LIBCPP_COMPILER_HAS_BUILTIN_CONSTANT_P and _LIBCPP_BUILTIN_CONSTANT_P to detect compile time constants, and optimze the code accordingly.

A planned usage example:
The implementation of basic_string::assign() can short-cut a compile time known short string assignent into a fast and compact inlined assignment:

  basic_string::assign(const value_type* __s) {
    if (_LIBCPP_BUILTIN_CONSTANT_P(__s[0]) && length(__s) < __min_cap) {
      copy(pointer(), _s, length(__s) + 1);
      set_size(length(__s));
    } else {
      // delegate / tail call out of line implementation
    }
  }


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73732

Files:
  libcxx/include/__config


Index: libcxx/include/__config
===================================================================
--- libcxx/include/__config
+++ libcxx/include/__config
@@ -496,6 +496,10 @@
 #define _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
 #endif
 
+#if __has_builtin(__builtin_constant_p)
+#define _LIBCPP_COMPILER_HAS_BUILTIN_CONSTANT_P
+#endif
+
 #if !__is_identifier(__has_unique_object_representations)
 #define _LIBCPP_HAS_UNIQUE_OBJECT_REPRESENTATIONS
 #endif
@@ -539,6 +543,10 @@
 #define _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
 #endif
 
+#if _GNUC_VER >= 700
+#define _LIBCPP_COMPILER_HAS_BUILTIN_CONSTANT_P
+#endif
+
 #if _GNUC_VER >= 700
 #define _LIBCPP_HAS_UNIQUE_OBJECT_REPRESENTATIONS
 #endif
@@ -1527,6 +1535,12 @@
 #  define _LIBCPP_FOPEN_CLOEXEC_MODE
 #endif
 
+#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_CONSTANT_P
+#define _LIBCPP_BUILTIN_CONSTANT_P(x) __builtin_constant_p(x)
+#else
+#define _LIBCPP_BUILTIN_CONSTANT_P(x) false
+#endif
+
 #endif // __cplusplus
 
 #endif // _LIBCPP_CONFIG


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73732.241523.patch
Type: text/x-patch
Size: 984 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200130/16936285/attachment.bin>


More information about the libcxx-commits mailing list