Make the __builtin_c[lt]zs builtins target independent.

There is really no reason to have these only available on x86. It's
just __builtin_c[tl]z for shorts.

Modernize the test while at it.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149183 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/Builtins.def b/include/clang/Basic/Builtins.def
index d49d611..b3b3c21 100644
--- a/include/clang/Basic/Builtins.def
+++ b/include/clang/Basic/Builtins.def
@@ -365,10 +365,12 @@
 BUILTIN(__builtin_signbitl, "iLd", "nc")
 
 // Builtins for arithmetic.
+BUILTIN(__builtin_clzs , "iUs"  , "nc")
 BUILTIN(__builtin_clz  , "iUi"  , "nc")
 BUILTIN(__builtin_clzl , "iULi" , "nc")
 BUILTIN(__builtin_clzll, "iULLi", "nc")
 // TODO: int clzimax(uintmax_t)
+BUILTIN(__builtin_ctzs , "iUs"  , "nc")
 BUILTIN(__builtin_ctz  , "iUi"  , "nc")
 BUILTIN(__builtin_ctzl , "iULi" , "nc")
 BUILTIN(__builtin_ctzll, "iULLi", "nc")
diff --git a/include/clang/Basic/BuiltinsX86.def b/include/clang/Basic/BuiltinsX86.def
index 2356b54..50ce477 100644
--- a/include/clang/Basic/BuiltinsX86.def
+++ b/include/clang/Basic/BuiltinsX86.def
@@ -593,11 +593,7 @@
 BUILTIN(__builtin_ia32_psrlv4di, "V4LLiV4LLiV4LLi", "")
 BUILTIN(__builtin_ia32_psrlv2di, "V2LLiV2LLiV2LLi", "")
 
-// LZCNT
-BUILTIN(__builtin_clzs, "UsUs", "")
-
 // BMI
-BUILTIN(__builtin_ctzs, "UsUs", "")
 BUILTIN(__builtin_ia32_bextr_u32, "UiUiUi", "")
 BUILTIN(__builtin_ia32_bextr_u64, "ULLiULLiULLi", "")
 
diff --git a/lib/CodeGen/CGBuiltin.cpp b/lib/CodeGen/CGBuiltin.cpp
index f4a5a82..acf95c8 100644
--- a/lib/CodeGen/CGBuiltin.cpp
+++ b/lib/CodeGen/CGBuiltin.cpp
@@ -229,6 +229,7 @@
 
     return RValue::get(Result);
   }
+  case Builtin::BI__builtin_ctzs:
   case Builtin::BI__builtin_ctz:
   case Builtin::BI__builtin_ctzl:
   case Builtin::BI__builtin_ctzll: {
@@ -245,6 +246,7 @@
                                      "cast");
     return RValue::get(Result);
   }
+  case Builtin::BI__builtin_clzs:
   case Builtin::BI__builtin_clz:
   case Builtin::BI__builtin_clzl:
   case Builtin::BI__builtin_clzll: {
@@ -2061,32 +2063,6 @@
 
   switch (BuiltinID) {
   default: return 0;
-  case X86::BI__builtin_clzs: {
-    Value *ArgValue = EmitScalarExpr(E->getArg(0));
-
-    llvm::Type *ArgType = ArgValue->getType();
-    Value *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType);
-
-    llvm::Type *ResultType = ConvertType(E->getType());
-    Value *Result = Builder.CreateCall2(F, ArgValue, Builder.getTrue());
-    if (Result->getType() != ResultType)
-      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
-                                     "cast");
-    return Result;
-  }
-  case X86::BI__builtin_ctzs: {
-    Value *ArgValue = EmitScalarExpr(E->getArg(0));
-
-    llvm::Type *ArgType = ArgValue->getType();
-    Value *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType);
-
-    llvm::Type *ResultType = ConvertType(E->getType());
-    Value *Result = Builder.CreateCall2(F, ArgValue, Builder.getTrue());
-    if (Result->getType() != ResultType)
-      Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true,
-                                     "cast");
-    return Result;
-  }
   case X86::BI__builtin_ia32_pslldi128:
   case X86::BI__builtin_ia32_psllqi128:
   case X86::BI__builtin_ia32_psllwi128:
diff --git a/test/CodeGen/2005-05-06-CountBuiltins.c b/test/CodeGen/2005-05-06-CountBuiltins.c
deleted file mode 100644
index 4c12100..0000000
--- a/test/CodeGen/2005-05-06-CountBuiltins.c
+++ /dev/null
@@ -1,17 +0,0 @@
-// RUN: %clang_cc1 %s -emit-llvm -o %t
-// RUN: not grep call*__builtin %t
-
-int G, H, I;
-void foo(int P) {
-  G = __builtin_clz(P);
-  H = __builtin_ctz(P);
-  I = __builtin_popcount(P);
-}
-
-long long g, h, i;
-void fooll(float P) {
-  g = __builtin_clzll(P);
-  g = __builtin_clzll(P);
-  h = __builtin_ctzll(P);
-  i = __builtin_popcountll(P);
-}
diff --git a/test/CodeGen/count-builtins.c b/test/CodeGen/count-builtins.c
new file mode 100644
index 0000000..e6133c5
--- /dev/null
+++ b/test/CodeGen/count-builtins.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s
+
+int leading, trailing, pop;
+
+void test_i16(short P) {
+  leading = __builtin_clzs(P);
+  trailing = __builtin_ctzs(P);
+
+// CHECK: @test_i16
+// CHECK: call i16 @llvm.ctlz.i16
+// CHECK: call i16 @llvm.cttz.i16
+}
+
+void test_i32(int P) {
+  leading = __builtin_clz(P);
+  trailing = __builtin_ctz(P);
+  pop = __builtin_popcount(P);
+
+// CHECK: @test_i32
+// CHECK: call i32 @llvm.ctlz.i32
+// CHECK: call i32 @llvm.cttz.i32
+// CHECK: call i32 @llvm.ctpop.i32
+}
+
+void test_i64(float P) {
+  leading = __builtin_clzll(P);
+  trailing = __builtin_ctzll(P);
+  pop = __builtin_popcountll(P);
+// CHECK: @test_i64
+// CHECK: call i64 @llvm.ctlz.i64
+// CHECK: call i64 @llvm.cttz.i64
+// CHECK: call i64 @llvm.ctpop.i64
+}