From ba96f37758412151885d50838d2031623fc2b918 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sat, 2 Jun 2018 18:03:35 +0000 Subject: [PATCH] Use __builtin for various mem* and b* (e.g. bzero) routines. Some of the routines were using artificially limited builtin already, drop the explicit limit. The use of builtins allows quite often allows the compiler to elide the call or most zeroing to begin with. For instance, if the target object is 32 bytes in size and gets zeroed + has 16 bytes initialized, the compiler can just add code to zero out the rest. Note not all the primites have asm variants and some of the existing ones are not optimized. Maintaines are strongly encourage to take a look (regardless of this change). --- sys/dev/cx/machdep.h | 1 - sys/libkern/bcmp.c | 2 +- sys/libkern/memcmp.c | 2 +- sys/libkern/memset.c | 2 +- sys/sys/libkern.h | 2 -- sys/sys/systm.h | 22 +++++++++------------- sys/sys/zutil.h | 2 -- 7 files changed, 12 insertions(+), 21 deletions(-) diff --git a/sys/dev/cx/machdep.h b/sys/dev/cx/machdep.h index a425af5b717d..393377d0b406 100644 --- a/sys/dev/cx/machdep.h +++ b/sys/dev/cx/machdep.h @@ -73,7 +73,6 @@ # include # include # include -# define memset(a,b,c) bzero (a,c) # define port_t int #ifndef _SYS_CDEFS_H_ diff --git a/sys/libkern/bcmp.c b/sys/libkern/bcmp.c index 1832922e37b8..40ef39953c86 100644 --- a/sys/libkern/bcmp.c +++ b/sys/libkern/bcmp.c @@ -44,7 +44,7 @@ typedef const unsigned long *culp; * bcmp -- vax cmpc3 instruction */ int -bcmp(const void *b1, const void *b2, size_t length) +(bcmp)(const void *b1, const void *b2, size_t length) { #if BYTE_ORDER == LITTLE_ENDIAN /* diff --git a/sys/libkern/memcmp.c b/sys/libkern/memcmp.c index e2f17906823e..127cf6df1106 100644 --- a/sys/libkern/memcmp.c +++ b/sys/libkern/memcmp.c @@ -41,7 +41,7 @@ __FBSDID("$FreeBSD$"); * Compare memory regions. */ int -memcmp(const void *s1, const void *s2, size_t n) +(memcmp)(const void *s1, const void *s2, size_t n) { if (n != 0) { const unsigned char *p1 = s1, *p2 = s2; diff --git a/sys/libkern/memset.c b/sys/libkern/memset.c index b2d64b21b90e..431011a34500 100644 --- a/sys/libkern/memset.c +++ b/sys/libkern/memset.c @@ -33,7 +33,7 @@ __FBSDID("$FreeBSD$"); #include void * -memset(void *b, int c, size_t len) +(memset)(void *b, int c, size_t len) { char *bb; diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h index 7af0ee9ecd61..0d6b818f693c 100644 --- a/sys/sys/libkern.h +++ b/sys/sys/libkern.h @@ -128,7 +128,6 @@ struct malloc_type; uint32_t arc4random(void); void arc4random_buf(void *, size_t); void arc4rand(void *, u_int, int); -int bcmp(const void *, const void *, size_t); int timingsafe_bcmp(const void *, const void *, size_t); void *bsearch(const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); @@ -160,7 +159,6 @@ int fnmatch(const char *, const char *, int); int locc(int, char *, u_int); void *memchr(const void *s, int c, size_t n); void *memcchr(const void *s, int c, size_t n); -int memcmp(const void *b1, const void *b2, size_t len); void *memmem(const void *l, size_t l_len, const void *s, size_t s_len); void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); diff --git a/sys/sys/systm.h b/sys/sys/systm.h index 4c9ab65f7c7d..fc81cd4e6421 100644 --- a/sys/sys/systm.h +++ b/sys/sys/systm.h @@ -259,25 +259,21 @@ void hexdump(const void *ptr, int length, const char *hdr, int flags); #define ovbcopy(f, t, l) bcopy((f), (t), (l)) void bcopy(const void * _Nonnull from, void * _Nonnull to, size_t len); -#define bcopy(from, to, len) ({ \ - if (__builtin_constant_p(len) && (len) <= 64) \ - __builtin_memmove((to), (from), (len)); \ - else \ - bcopy((from), (to), (len)); \ -}) +#define bcopy(from, to, len) __builtin_memmove((to), (from), (len)) void bzero(void * _Nonnull buf, size_t len); -#define bzero(buf, len) ({ \ - if (__builtin_constant_p(len) && (len) <= 64) \ - __builtin_memset((buf), 0, (len)); \ - else \ - bzero((buf), (len)); \ -}) +#define bzero(buf, len) __builtin_memset((buf), 0, (len)) void explicit_bzero(void * _Nonnull, size_t); +int bcmp(const void *b1, const void *b2, size_t len); +#define bcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len)) void *memset(void * _Nonnull buf, int c, size_t len); +#define memset(buf, c, len) __builtin_memset((buf), (c), (len)) void *memcpy(void * _Nonnull to, const void * _Nonnull from, size_t len); -#define memcpy(to, from, len) __builtin_memcpy(to, from, len) +#define memcpy(to, from, len) __builtin_memcpy((to), (from), (len)) void *memmove(void * _Nonnull dest, const void * _Nonnull src, size_t n); +#define memmove(dest, src, n) __builtin_memmove((dest), (src), (n)) +int memcmp(const void *b1, const void *b2, size_t len); +#define memcmp(b1, b2, len) __builtin_memcmp((b1), (b2), (len)) int copystr(const void * _Nonnull __restrict kfaddr, void * _Nonnull __restrict kdaddr, size_t len, diff --git a/sys/sys/zutil.h b/sys/sys/zutil.h index 733185f2d00e..4b9541d4dab7 100644 --- a/sys/sys/zutil.h +++ b/sys/sys/zutil.h @@ -32,8 +32,6 @@ #include #include # define HAVE_MEMCPY -# define memset(d, v, n) bzero((d), (n)) -# define memcmp bcmp #else #if defined(__KERNEL__)