[libcxx-commits] [libcxx] 07eb82f - Add _LIBCPP_BUILTIN_CONSTANT_P support.

Martijn Vels via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 26 08:04:56 PST 2020


Author: Martijn Vels
Date: 2020-02-26T11:03:51-05:00
New Revision: 07eb82fc06cc41a64d136d046e6b30565c3e5579

URL: https://github.com/llvm/llvm-project/commit/07eb82fc06cc41a64d136d046e6b30565c3e5579
DIFF: https://github.com/llvm/llvm-project/commit/07eb82fc06cc41a64d136d046e6b30565c3e5579.diff

LOG: Add _LIBCPP_BUILTIN_CONSTANT_P support.

Summary:
This change adds the macros _LIBCPP_COMPILER_HAS_BUILTIN_CONSTANT 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
  }
}
```

Subscribers: christof, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D73732

Added: 
    

Modified: 
    libcxx/include/__config

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__config b/libcxx/include/__config
index df3ce8ae87f6..6cd12669fd34 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -496,6 +496,10 @@ typedef __char32_t char32_t;
 #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
@@ -537,9 +541,7 @@ typedef __char32_t char32_t;
 
 #if _GNUC_VER >= 700
 #define _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
-#endif
-
-#if _GNUC_VER >= 700
+#define _LIBCPP_COMPILER_HAS_BUILTIN_CONSTANT_P
 #define _LIBCPP_HAS_UNIQUE_OBJECT_REPRESENTATIONS
 #endif
 
@@ -1533,6 +1535,12 @@ _LIBCPP_FUNC_VIS extern "C" void __sanitizer_annotate_contiguous_container(
 #  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


        


More information about the libcxx-commits mailing list