r221065 - Avoid undefined behavior in the x86 bmi header file by explicitly checking for 0 before calling __builtin_ctz. Without this the optimizers may take advantage of the undefined behavior and produce incorrect results. LLVM itself still needs to be taught to merge the zero check into the llvm.cttz with defined zero behavior.

Craig Topper craig.topper at gmail.com
Sat Nov 1 15:50:54 PDT 2014


Author: ctopper
Date: Sat Nov  1 17:50:54 2014
New Revision: 221065

URL: http://llvm.org/viewvc/llvm-project?rev=221065&view=rev
Log:
Avoid undefined behavior in the x86 bmi header file by explicitly checking for 0 before calling __builtin_ctz. Without this the optimizers may take advantage of the undefined behavior and produce incorrect results. LLVM itself still needs to be taught to merge the zero check into the llvm.cttz with defined zero behavior.

Modified:
    cfe/trunk/lib/Headers/bmiintrin.h

Modified: cfe/trunk/lib/Headers/bmiintrin.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/bmiintrin.h?rev=221065&r1=221064&r2=221065&view=diff
==============================================================================
--- cfe/trunk/lib/Headers/bmiintrin.h (original)
+++ cfe/trunk/lib/Headers/bmiintrin.h Sat Nov  1 17:50:54 2014
@@ -43,7 +43,7 @@
 static __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
 __tzcnt_u16(unsigned short __X)
 {
-  return __builtin_ctzs(__X);
+  return __X ? __builtin_ctzs(__X) : 16;
 }
 
 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
@@ -87,7 +87,7 @@ __blsr_u32(unsigned int __X)
 static __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
 __tzcnt_u32(unsigned int __X)
 {
-  return __builtin_ctz(__X);
+  return __X ? __builtin_ctz(__X) : 32;
 }
 
 #ifdef __x86_64__
@@ -140,7 +140,7 @@ __blsr_u64(unsigned long long __X)
 static __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
 __tzcnt_u64(unsigned long long __X)
 {
-  return __builtin_ctzll(__X);
+  return __X ? __builtin_ctzll(__X) : 64;
 }
 
 #endif /* __x86_64__ */





More information about the cfe-commits mailing list