[compiler-rt] r208776 - [asan] use some LIKELY/UNLIKELY

Sergey Matveev earthdok at google.com
Wed May 14 10:21:43 PDT 2014


are there benchmarks?


On Wed, May 14, 2014 at 6:03 PM, Kostya Serebryany <kcc at google.com> wrote:

> Author: kcc
> Date: Wed May 14 09:03:31 2014
> New Revision: 208776
>
> URL: http://llvm.org/viewvc/llvm-project?rev=208776&view=rev
> Log:
> [asan] use some LIKELY/UNLIKELY
>
> Modified:
>     compiler-rt/trunk/lib/asan/asan_allocator2.cc
>     compiler-rt/trunk/lib/asan/asan_interceptors.cc
>     compiler-rt/trunk/lib/asan/asan_interceptors.h
>     compiler-rt/trunk/lib/asan/asan_malloc_linux.cc
>     compiler-rt/trunk/lib/asan/asan_malloc_mac.cc
>     compiler-rt/trunk/lib/asan/asan_rtl.cc
>     compiler-rt/trunk/lib/asan/asan_stack.h
>     compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
>
> Modified: compiler-rt/trunk/lib/asan/asan_allocator2.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_allocator2.cc?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_allocator2.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_allocator2.cc Wed May 14 09:03:31 2014
> @@ -281,7 +281,7 @@ void ReInitializeAllocator() {
>
>  static void *Allocate(uptr size, uptr alignment, StackTrace *stack,
>                        AllocType alloc_type, bool can_fill) {
> -  if (!asan_inited)
> +  if (UNLIKELY(!asan_inited))
>      AsanInitFromRtl();
>    Flags &fl = *flags();
>    CHECK(stack);
>
> Modified: compiler-rt/trunk/lib/asan/asan_interceptors.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.cc?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_interceptors.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_interceptors.cc Wed May 14 09:03:31
> 2014
> @@ -117,13 +117,15 @@ DECLARE_REAL_AND_INTERCEPTOR(void, free,
>  #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \
>    ASAN_WRITE_RANGE(ptr, size)
>  #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size)
> ASAN_READ_RANGE(ptr, size)
> -#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)                       \
> -  do {                                                                 \
> -    if (asan_init_is_running) return REAL(func)(__VA_ARGS__);          \
> -    ctx = 0;                                                           \
> -    (void) ctx;                                                        \
> -    if (SANITIZER_MAC && !asan_inited) return REAL(func)(__VA_ARGS__); \
> -    ENSURE_ASAN_INITED();                                              \
> +#define COMMON_INTERCEPTOR_ENTER(ctx, func, ...)
>       \
> +  do {
>       \
> +    if (asan_init_is_running)
>      \
> +      return REAL(func)(__VA_ARGS__);
>      \
> +    ctx = 0;
>       \
> +    (void) ctx;
>      \
> +    if (SANITIZER_MAC && UNLIKELY(!asan_inited))
>       \
> +      return REAL(func)(__VA_ARGS__);
>      \
> +    ENSURE_ASAN_INITED();
>      \
>    } while (false)
>  #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \
>    do {                                         \
> @@ -328,7 +330,7 @@ static inline int CharCmp(unsigned char
>  }
>
>  INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
> -  if (!asan_inited) return internal_memcmp(a1, a2, size);
> +  if (UNLIKELY(!asan_inited)) return internal_memcmp(a1, a2, size);
>    ENSURE_ASAN_INITED();
>    if (flags()->replace_intrin) {
>      if (flags()->strict_memcmp) {
> @@ -356,7 +358,7 @@ INTERCEPTOR(int, memcmp, const void *a1,
>  }
>
>  void *__asan_memcpy(void *to, const void *from, uptr size) {
> -  if (!asan_inited) return internal_memcpy(to, from, size);
> +  if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size);
>    // memcpy is called during __asan_init() from the internals
>    // of printf(...).
>    if (asan_init_is_running) {
> @@ -376,7 +378,7 @@ void *__asan_memcpy(void *to, const void
>  }
>
>  void *__asan_memset(void *block, int c, uptr size) {
> -  if (!asan_inited) return internal_memset(block, c, size);
> +  if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size);
>    // memset is called inside Printf.
>    if (asan_init_is_running) {
>      return REAL(memset)(block, c, size);
> @@ -389,7 +391,7 @@ void *__asan_memset(void *block, int c,
>  }
>
>  void *__asan_memmove(void *to, const void *from, uptr size) {
> -  if (!asan_inited)
> +  if (UNLIKELY(!asan_inited))
>      return internal_memmove(to, from, size);
>    ENSURE_ASAN_INITED();
>    if (flags()->replace_intrin) {
> @@ -422,7 +424,7 @@ INTERCEPTOR(void*, memset, void *block,
>  }
>
>  INTERCEPTOR(char*, strchr, const char *str, int c) {
> -  if (!asan_inited) return internal_strchr(str, c);
> +  if (UNLIKELY(!asan_inited)) return internal_strchr(str, c);
>    // strchr is called inside create_purgeable_zone() when
> MallocGuardEdges=1 is
>    // used.
>    if (asan_init_is_running) {
> @@ -491,7 +493,7 @@ INTERCEPTOR(char*, strncat, char *to, co
>
>  INTERCEPTOR(char*, strcpy, char *to, const char *from) {  // NOLINT
>  #if SANITIZER_MAC
> -  if (!asan_inited) return REAL(strcpy)(to, from);  // NOLINT
> +  if (UNLIKELY(!asan_inited)) return REAL(strcpy)(to, from);  // NOLINT
>  #endif
>    // strcpy is called from malloc_default_purgeable_zone()
>    // in __asan::ReplaceSystemAlloc() on Mac.
> @@ -510,7 +512,7 @@ INTERCEPTOR(char*, strcpy, char *to, con
>
>  #if ASAN_INTERCEPT_STRDUP
>  INTERCEPTOR(char*, strdup, const char *s) {
> -  if (!asan_inited) return internal_strdup(s);
> +  if (UNLIKELY(!asan_inited)) return internal_strdup(s);
>    ENSURE_ASAN_INITED();
>    uptr length = REAL(strlen)(s);
>    if (flags()->replace_str) {
> @@ -524,7 +526,7 @@ INTERCEPTOR(char*, strdup, const char *s
>  #endif
>
>  INTERCEPTOR(uptr, strlen, const char *s) {
> -  if (!asan_inited) return internal_strlen(s);
> +  if (UNLIKELY(!asan_inited)) return internal_strlen(s);
>    // strlen is called from malloc_default_purgeable_zone()
>    // in __asan::ReplaceSystemAlloc() on Mac.
>    if (asan_init_is_running) {
> @@ -606,7 +608,7 @@ INTERCEPTOR(long, strtol, const char *np
>
>  INTERCEPTOR(int, atoi, const char *nptr) {
>  #if SANITIZER_MAC
> -  if (!asan_inited) return REAL(atoi)(nptr);
> +  if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr);
>  #endif
>    ENSURE_ASAN_INITED();
>    if (!flags()->replace_str) {
> @@ -625,7 +627,7 @@ INTERCEPTOR(int, atoi, const char *nptr)
>
>  INTERCEPTOR(long, atol, const char *nptr) {  // NOLINT
>  #if SANITIZER_MAC
> -  if (!asan_inited) return REAL(atol)(nptr);
> +  if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr);
>  #endif
>    ENSURE_ASAN_INITED();
>    if (!flags()->replace_str) {
> @@ -682,7 +684,7 @@ static void AtCxaAtexit(void *unused) {
>  INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
>              void *dso_handle) {
>  #if SANITIZER_MAC
> -  if (!asan_inited) return REAL(__cxa_atexit)(func, arg, dso_handle);
> +  if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg,
> dso_handle);
>  #endif
>    ENSURE_ASAN_INITED();
>    int res = REAL(__cxa_atexit)(func, arg, dso_handle);
>
> Modified: compiler-rt/trunk/lib/asan/asan_interceptors.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_interceptors.h?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_interceptors.h (original)
> +++ compiler-rt/trunk/lib/asan/asan_interceptors.h Wed May 14 09:03:31 2014
> @@ -96,7 +96,7 @@ void InitializeAsanInterceptors();
>
>  #define ENSURE_ASAN_INITED() do { \
>    CHECK(!asan_init_is_running); \
> -  if (!asan_inited) { \
> +  if (UNLIKELY(!asan_inited)) { \
>      AsanInitFromRtl(); \
>    } \
>  } while (0)
>
> Modified: compiler-rt/trunk/lib/asan/asan_malloc_linux.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_malloc_linux.cc?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_malloc_linux.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_malloc_linux.cc Wed May 14 09:03:31
> 2014
> @@ -77,7 +77,7 @@ INTERCEPTOR(void*, malloc, uptr size) {
>  }
>
>  INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
> -  if (!asan_inited) {
> +  if (UNLIKELY(!asan_inited)) {
>      // Hack: dlsym calls calloc before REAL(calloc) is retrieved from
> dlsym.
>      const uptr kCallocPoolSize = 1024;
>      static uptr calloc_memory_for_dlsym[kCallocPoolSize];
>
> Modified: compiler-rt/trunk/lib/asan/asan_malloc_mac.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_malloc_mac.cc?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_malloc_mac.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_malloc_mac.cc Wed May 14 09:03:31 2014
> @@ -159,7 +159,7 @@ size_t mz_size(malloc_zone_t* zone, cons
>  }
>
>  void *mz_malloc(malloc_zone_t *zone, size_t size) {
> -  if (!asan_inited) {
> +  if (UNLIKELY(!asan_inited)) {
>      CHECK(system_malloc_zone);
>      return malloc_zone_malloc(system_malloc_zone, size);
>    }
> @@ -168,7 +168,7 @@ void *mz_malloc(malloc_zone_t *zone, siz
>  }
>
>  void *mz_calloc(malloc_zone_t *zone, size_t nmemb, size_t size) {
> -  if (!asan_inited) {
> +  if (UNLIKELY(!asan_inited)) {
>      // Hack: dlsym calls calloc before REAL(calloc) is retrieved from
> dlsym.
>      const size_t kCallocPoolSize = 1024;
>      static uptr calloc_memory_for_dlsym[kCallocPoolSize];
> @@ -184,7 +184,7 @@ void *mz_calloc(malloc_zone_t *zone, siz
>  }
>
>  void *mz_valloc(malloc_zone_t *zone, size_t size) {
> -  if (!asan_inited) {
> +  if (UNLIKELY(!asan_inited)) {
>      CHECK(system_malloc_zone);
>      return malloc_zone_valloc(system_malloc_zone, size);
>    }
> @@ -242,7 +242,7 @@ void mz_destroy(malloc_zone_t* zone) {
>  #if defined(MAC_OS_X_VERSION_10_6) && \
>      MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
>  void *mz_memalign(malloc_zone_t *zone, size_t align, size_t size) {
> -  if (!asan_inited) {
> +  if (UNLIKELY(!asan_inited)) {
>      CHECK(system_malloc_zone);
>      return malloc_zone_memalign(system_malloc_zone, align, size);
>    }
>
> Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
> +++ compiler-rt/trunk/lib/asan/asan_rtl.cc Wed May 14 09:03:31 2014
> @@ -552,7 +552,7 @@ static void PrintAddressSpaceLayout() {
>  }
>
>  static void AsanInitInternal() {
> -  if (asan_inited) return;
> +  if (LIKELY(asan_inited)) return;
>    SanitizerToolName = "AddressSanitizer";
>    CHECK(!asan_init_is_running && "ASan init calls itself!");
>    asan_init_is_running = true;
> @@ -708,7 +708,7 @@ public:  // NOLINT
>    AsanInitializer() {
>      AsanCheckIncompatibleRT();
>      AsanCheckDynamicRTPrereqs();
> -    if (!asan_inited)
> +    if (UNLIKELY(!asan_inited))
>        __asan_init();
>    }
>  };
>
> Modified: compiler-rt/trunk/lib/asan/asan_stack.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_stack.h?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/asan/asan_stack.h (original)
> +++ compiler-rt/trunk/lib/asan/asan_stack.h Wed May 14 09:03:31 2014
> @@ -32,7 +32,7 @@ void GetStackTraceWithPcBpAndContext(Sta
>  #else
>    AsanThread *t;
>    stack->size = 0;
> -  if (asan_inited) {
> +  if (LIKELY(asan_inited)) {
>      if ((t = GetCurrentThread()) && !t->isUnwinding()) {
>        uptr stack_top = t->stack_top();
>        uptr stack_bottom = t->stack_bottom();
>
> Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
> URL:
> http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h?rev=208776&r1=208775&r2=208776&view=diff
>
> ==============================================================================
> --- compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h
> (original)
> +++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_internal_defs.h Wed
> May 14 09:03:31 2014
> @@ -199,7 +199,7 @@ void NORETURN CheckFailed(const char *fi
>
>  // Check macro
>  #define RAW_CHECK_MSG(expr, msg) do { \
> -  if (!(expr)) { \
> +  if (UNLIKELY(!(expr))) { \
>      RawWrite(msg); \
>      Die(); \
>    } \
> @@ -211,7 +211,7 @@ void NORETURN CheckFailed(const char *fi
>    do { \
>      __sanitizer::u64 v1 = (u64)(c1); \
>      __sanitizer::u64 v2 = (u64)(c2); \
> -    if (!(v1 op v2)) \
> +    if (UNLIKELY(!(v1 op v2))) \
>        __sanitizer::CheckFailed(__FILE__, __LINE__, \
>          "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
>    } while (false) \
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140514/a0376d95/attachment.html>


More information about the llvm-commits mailing list