[cfe-commits] r147340 - in /cfe/trunk: include/clang/Driver/Options.td lib/Basic/Targets.cpp lib/Headers/popcntintrin.h lib/Headers/smmintrin.h lib/Headers/x86intrin.h test/CodeGen/popcnt-builtins.c

Craig Topper craig.topper at gmail.com
Thu Dec 29 08:10:47 PST 2011


Author: ctopper
Date: Thu Dec 29 10:10:46 2011
New Revision: 147340

URL: http://llvm.org/viewvc/llvm-project?rev=147340&view=rev
Log:
Add popcnt feature flag to match gcc. This flag is implied when sse42 is enabled, but can be disabled separately. Move popcnt intrinsics to popcntintrin.h to match gcc.

Added:
    cfe/trunk/lib/Headers/popcntintrin.h
    cfe/trunk/test/CodeGen/popcnt-builtins.c
Modified:
    cfe/trunk/include/clang/Driver/Options.td
    cfe/trunk/lib/Basic/Targets.cpp
    cfe/trunk/lib/Headers/smmintrin.h
    cfe/trunk/lib/Headers/x86intrin.h

Modified: cfe/trunk/include/clang/Driver/Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/Options.td?rev=147340&r1=147339&r2=147340&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/Options.td (original)
+++ cfe/trunk/include/clang/Driver/Options.td Thu Dec 29 10:10:46 2011
@@ -615,6 +615,7 @@
 def mno_lzcnt : Flag<"-mno-lzcnt">, Group<m_x86_Features_Group>;
 def mno_bmi : Flag<"-mno-bmi">, Group<m_x86_Features_Group>;
 def mno_bmi2 : Flag<"-mno-bmi2">, Group<m_x86_Features_Group>;
+def mno_popcnt : Flag<"-mno-popcnt">, Group<m_x86_Features_Group>;
 
 def mno_thumb : Flag<"-mno-thumb">, Group<m_Group>;
 def marm : Flag<"-marm">, Alias<mno_thumb>;
@@ -643,6 +644,7 @@
 def mlzcnt : Flag<"-mlzcnt">, Group<m_x86_Features_Group>;
 def mbmi : Flag<"-mbmi">, Group<m_x86_Features_Group>;
 def mbmi2 : Flag<"-mbmi2">, Group<m_x86_Features_Group>;
+def mpopcnt : Flag<"-mpopcnt">, Group<m_x86_Features_Group>;
 def mthumb : Flag<"-mthumb">, Group<m_Group>;
 def mtune_EQ : Joined<"-mtune=">, Group<m_Group>;
 def multi__module : Flag<"-multi_module">;

Modified: cfe/trunk/lib/Basic/Targets.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Targets.cpp?rev=147340&r1=147339&r2=147340&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Targets.cpp (original)
+++ cfe/trunk/lib/Basic/Targets.cpp Thu Dec 29 10:10:46 2011
@@ -1198,6 +1198,7 @@
   bool HasLZCNT;
   bool HasBMI;
   bool HasBMI2;
+  bool HasPOPCNT;
 
   /// \brief Enumeration of all of the X86 CPUs supported by Clang.
   ///
@@ -1335,7 +1336,7 @@
   X86TargetInfo(const std::string& triple)
     : TargetInfo(triple), SSELevel(NoSSE), MMX3DNowLevel(NoMMX3DNow),
       HasAES(false), HasAVX(false), HasAVX2(false), HasLZCNT(false),
-      HasBMI(false), HasBMI2(false), CPU(CK_Generic) {
+      HasBMI(false), HasBMI2(false), HasPOPCNT(false), CPU(CK_Generic) {
     BigEndian = false;
     LongDoubleFormat = &llvm::APFloat::x87DoubleExtended;
   }
@@ -1519,6 +1520,7 @@
   Features["lzcnt"] = false;
   Features["bmi"] = false;
   Features["bmi2"] = false;
+  Features["popcnt"] = false;
 
   // FIXME: This *really* should not be here.
 
@@ -1669,7 +1671,8 @@
         Features["ssse3"] = true;
     else if (Name == "sse4" || Name == "sse4.2")
       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
-        Features["ssse3"] = Features["sse41"] = Features["sse42"] = true;
+        Features["ssse3"] = Features["sse41"] = Features["sse42"] =
+        Features["popcnt"] = true;
     else if (Name == "sse4.1")
       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
         Features["ssse3"] = Features["sse41"] = true;
@@ -1682,11 +1685,11 @@
     else if (Name == "avx")
       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
-        Features["avx"] = true;
+        Features["popcnt"] = Features["avx"] = true;
     else if (Name == "avx2")
       Features["mmx"] = Features["sse"] = Features["sse2"] = Features["sse3"] =
         Features["ssse3"] = Features["sse41"] = Features["sse42"] =
-        Features["avx"] = Features["avx2"] = true;
+        Features["popcnt"] = Features["avx"] = Features["avx2"] = true;
     else if (Name == "sse4a")
       Features["mmx"] = Features["sse4a"] = true;
     else if (Name == "lzcnt")
@@ -1695,6 +1698,8 @@
       Features["bmi"] = true;
     else if (Name == "bmi2")
       Features["bmi2"] = true;
+    else if (Name == "popcnt")
+      Features["popcnt"] = true;
   } else {
     if (Name == "mmx")
       Features["mmx"] = Features["3dnow"] = Features["3dnowa"] = false;
@@ -1731,6 +1736,8 @@
       Features["bmi"] = false;
     else if (Name == "bmi2")
       Features["bmi2"] = false;
+    else if (Name == "popcnt")
+      Features["popcnt"] = false;
   }
 
   return true;
@@ -1765,6 +1772,11 @@
       continue;
     }
 
+    if (Features[i].substr(1) == "popcnt") {
+      HasPOPCNT = true;
+      continue;
+    }
+
     // FIXME: Not sure yet how to treat AVX in regard to SSE levels.
     // For now let it be enabled together with other SSE levels.
     if (Features[i].substr(1) == "avx2") {
@@ -1996,6 +2008,9 @@
   if (HasBMI2)
     Builder.defineMacro("__BMI2__");
 
+  if (HasPOPCNT)
+    Builder.defineMacro("__POPCNT__");
+
   // Each case falls through to the previous one here.
   switch (SSELevel) {
   case SSE42:

Added: cfe/trunk/lib/Headers/popcntintrin.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/popcntintrin.h?rev=147340&view=auto
==============================================================================
--- cfe/trunk/lib/Headers/popcntintrin.h (added)
+++ cfe/trunk/lib/Headers/popcntintrin.h Thu Dec 29 10:10:46 2011
@@ -0,0 +1,45 @@
+/*===---- popcntintrin.h - POPCNT intrinsics -------------------------------===
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ *===-----------------------------------------------------------------------===
+ */
+
+#ifndef __POPCNT__
+#error "POPCNT instruction set not enabled"
+#endif
+
+#ifndef _POPCNTINTRIN_H
+#define _POPCNTINTRIN_H
+
+static __inline__ int __attribute__((__always_inline__, __nodebug__))
+_mm_popcnt_u32(unsigned int __A)
+{
+  return __builtin_popcount(__A);
+}
+
+#ifdef __x86_64__
+static __inline__ long long __attribute__((__always_inline__, __nodebug__))
+_mm_popcnt_u64(unsigned long long __A)
+{
+  return __builtin_popcountll(__A);
+}
+#endif /* __x86_64__ */
+
+#endif /* _POPCNTINTRIN_H */

Modified: cfe/trunk/lib/Headers/smmintrin.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/smmintrin.h?rev=147340&r1=147339&r2=147340&view=diff
==============================================================================
--- cfe/trunk/lib/Headers/smmintrin.h (original)
+++ cfe/trunk/lib/Headers/smmintrin.h Thu Dec 29 10:10:46 2011
@@ -428,20 +428,9 @@
 }
 #endif /* __x86_64__ */
 
-/* SSE4.2 Population Count.  */
-static __inline__ int __attribute__((__always_inline__, __nodebug__))
-_mm_popcnt_u32(unsigned int __A)
-{
-  return __builtin_popcount(__A);
-}
-
-#ifdef __x86_64__
-static __inline__ long long __attribute__((__always_inline__, __nodebug__))
-_mm_popcnt_u64(unsigned long long __A)
-{
-  return __builtin_popcountll(__A);
-}
-#endif /* __x86_64__ */
+#ifdef __POPCNT__
+#include <popcntintrin.h>
+#endif
 
 #endif /* __SSE4_2__ */
 #endif /* __SSE4_1__ */

Modified: cfe/trunk/lib/Headers/x86intrin.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Headers/x86intrin.h?rev=147340&r1=147339&r2=147340&view=diff
==============================================================================
--- cfe/trunk/lib/Headers/x86intrin.h (original)
+++ cfe/trunk/lib/Headers/x86intrin.h Thu Dec 29 10:10:46 2011
@@ -38,6 +38,10 @@
 #include <lzcntintrin.h>
 #endif
 
-// FIXME: SSE4A, 3dNOW, FMA4, XOP, LWP, ABM, POPCNT
+#ifdef __POPCNT__
+#include <popcntintrin.h>
+#endif
+
+// FIXME: SSE4A, 3dNOW, FMA4, XOP, LWP, ABM
 
 #endif /* __X86INTRIN_H */

Added: cfe/trunk/test/CodeGen/popcnt-builtins.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/popcnt-builtins.c?rev=147340&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/popcnt-builtins.c (added)
+++ cfe/trunk/test/CodeGen/popcnt-builtins.c Thu Dec 29 10:10:46 2011
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -O3 -triple=x86_64-apple-darwin -target-feature +popcnt -emit-llvm -o - | FileCheck %s
+
+// Don't include mm_malloc.h, it's system specific.
+#define __MM_MALLOC_H
+
+#include <x86intrin.h>
+
+unsigned int test_mm_popcnt_u32(unsigned int __X) {
+  // CHECK: @llvm.ctpop.i32
+  return _mm_popcnt_u32(__X);
+}
+
+unsigned long long test_mm_popcnt_u64(unsigned long long __X) {
+  // CHECK: @llvm.ctpop.i64
+  return _mm_popcnt_u64(__X);
+}





More information about the cfe-commits mailing list