https://github.com/xakep8 updated 
https://github.com/llvm/llvm-project/pull/214931

>From 23168137fd397e589f03922ba0216061fdc9faf1 Mon Sep 17 00:00:00 2001
From: Kunal Dubey <[email protected]>
Date: Sat, 8 Aug 2026 10:19:23 +0530
Subject: [PATCH 1/6] [CIR] Lowering __builtin_stdc_trailing_zeros

Added CIRGen support for __builtin_stdc_trailing_zeros by lowering it to
cir.ctz with zero behaviour defined, matching with the C23 stdbit
semantics.

Added CodeGen test for the same.
---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp           |  7 +++++++
 clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c | 10 ++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 1efe2b81d5cae..2eb9d6c3bb661 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -1247,6 +1247,13 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
                                            getTarget().isCLZForZeroUndef());
   case Builtin::BI__builtin_ctzg:
     return emitBuiltinBitOpWithFallback<cir::BitCtzOp>(*this, e);
+  case Builtin::BIstdc_trailing_zeros_uc:
+  case Builtin::BIstdc_trailing_zeros_us:
+  case Builtin::BIstdc_trailiing_zeros_ui:
+  case Builtin::BIstdc_trailing_zeros_ul:
+  case Builtin::BIstdc_trailing_zeros_ull:
+  case Builtin::BI__builtin_stdc_trailing_zeros:
+    return emitBuiltinBitOp<cir::BitCtzOp>(*this, e, /*poisonZero=*/false);
 
   case Builtin::BI__builtin_clzs:
   case Builtin::BI__builtin_clz:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
new file mode 100644
index 0000000000000..6d285c259a96f
--- /dev/null
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o 
- | FileCheck %s
+
+void test_stdc_trailing_zeros(unsigned long long x) {
+  int cnt = __builtin_stdc_trailing_zeros(x);
+  (void)cnt;
+}
+
+// CHECK-LABEL: test_stdc_trailing_zeros
+// CHECK: cir.ctz
+// CHECK-NOT: poison_zero

>From 7f6b40a228b9c986c4af24b1327aa5c9e47dbac6 Mon Sep 17 00:00:00 2001
From: Kunal Dubey <[email protected]>
Date: Sat, 8 Aug 2026 11:31:41 +0530
Subject: [PATCH 2/6] [CIR] Lowering __builtin_stdc_leading_zeros

Added CIRGen support for __builtin_stdc_leading_zeros by lowering to
cir.clz with zero behaviour defined, matching C23 stdbit semantics.

Added CodeGen test for the same.
---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp       |  8 ++++++++
 .../CIR/CodeGenBuiltins/builtin-stdc-bit.c    | 19 +++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 2eb9d6c3bb661..bbc82f33571d4 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -1255,6 +1255,14 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BI__builtin_stdc_trailing_zeros:
     return emitBuiltinBitOp<cir::BitCtzOp>(*this, e, /*poisonZero=*/false);
 
+  case Builtin::BIstdc_leading_zeros_uc:
+  case Builtin::BIstdc_leading_zeros_us:
+  case Builtin::BIstdc_leading_zeros_ui:
+  case Builtin::BIstdc_leading_zeros_ul:
+  case Builtin::BIstdc_leading_zeros_ull:
+  case Builtin::BI__builtin_stdc_leading_zeros:
+    return emitBuiltinBitOp<cir::BitClzOp>(*this, e, /*poisonZero=*/false);
+
   case Builtin::BI__builtin_clzs:
   case Builtin::BI__builtin_clz:
   case Builtin::BI__builtin_clzl:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
index 6d285c259a96f..1f127821673d2 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
@@ -8,3 +8,22 @@ void test_stdc_trailing_zeros(unsigned long long x) {
 // CHECK-LABEL: test_stdc_trailing_zeros
 // CHECK: cir.ctz
 // CHECK-NOT: poison_zero
+// CHECK: cir.return
+
+void test_stdc_leading_zeros(unsigned x) {
+  return __builtin_stdc_trailing_zeros(x);
+}
+
+// CHECK-LABEL: test_stdc_leading_zeros
+// CHECK: cir.clz
+// CHECK-NOT: poison_zero
+// CHECK: cir.return
+
+void test_stdc_leading_zeros_ui(unsigned x) {
+  return stdc_leading_zeros_ui(x);
+}
+
+// CHECK-LABEL: test_stdc_leading_zeros_ui
+// CHECK: cir.clz
+// CHECK-NOT: poison_zero
+// CHECK: cir.return

>From f7059f999f678712d76ef1057914c92cbae71036 Mon Sep 17 00:00:00 2001
From: Kunal Dubey <[email protected]>
Date: Sun, 9 Aug 2026 16:33:16 +0530
Subject: [PATCH 3/6] [CIR] Lowering __builtin_stdc_count_ones

Added CIRGen support for lowering of __builtin_stdc_count_ones by
lowering to cir.popcount, matching with the C23 stdbit semantics.

Added test for the same.
---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp       | 10 ++++++++-
 .../CIR/CodeGenBuiltins/builtin-stdc-bit.c    | 22 ++++++++++++++++---
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index bbc82f33571d4..d27ace31cef78 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -1249,7 +1249,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
     return emitBuiltinBitOpWithFallback<cir::BitCtzOp>(*this, e);
   case Builtin::BIstdc_trailing_zeros_uc:
   case Builtin::BIstdc_trailing_zeros_us:
-  case Builtin::BIstdc_trailiing_zeros_ui:
+  case Builtin::BIstdc_trailing_zeros_ui:
   case Builtin::BIstdc_trailing_zeros_ul:
   case Builtin::BIstdc_trailing_zeros_ull:
   case Builtin::BI__builtin_stdc_trailing_zeros:
@@ -1263,6 +1263,14 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BI__builtin_stdc_leading_zeros:
     return emitBuiltinBitOp<cir::BitClzOp>(*this, e, /*poisonZero=*/false);
 
+  case Builtin::BIstdc_count_ones_uc:
+  case Builtin::BIstdc_count_ones_us:
+  case Builtin::BIstdc_count_ones_ui:
+  case Builtin::BIstdc_count_ones_ul:
+  case Builtin::BIstdc_count_ones_ull:
+  case Builtin::BI__builtin_stdc_count_ones:
+    return emitBuiltinBitOp<cir::BitPopcountOp>(*this, e);
+
   case Builtin::BI__builtin_clzs:
   case Builtin::BI__builtin_clz:
   case Builtin::BI__builtin_clzl:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
index 1f127821673d2..e9d5b25a10f0e 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
@@ -10,8 +10,8 @@ void test_stdc_trailing_zeros(unsigned long long x) {
 // CHECK-NOT: poison_zero
 // CHECK: cir.return
 
-void test_stdc_leading_zeros(unsigned x) {
-  return __builtin_stdc_trailing_zeros(x);
+unsigned test_stdc_leading_zeros(unsigned x) {
+  return __builtin_stdc_leading_zeros(x);
 }
 
 // CHECK-LABEL: test_stdc_leading_zeros
@@ -19,7 +19,7 @@ void test_stdc_leading_zeros(unsigned x) {
 // CHECK-NOT: poison_zero
 // CHECK: cir.return
 
-void test_stdc_leading_zeros_ui(unsigned x) {
+unsigned test_stdc_leading_zeros_ui(unsigned x) {
   return stdc_leading_zeros_ui(x);
 }
 
@@ -27,3 +27,19 @@ void test_stdc_leading_zeros_ui(unsigned x) {
 // CHECK: cir.clz
 // CHECK-NOT: poison_zero
 // CHECK: cir.return
+
+unsigned test_stdc_count_ones(unsigned x) {
+  return __builtin_stdc_count_ones(x);
+}
+
+// CHECK-LABEL: test_stdc_count_ones
+// CHECK: cir.popcount
+// CHECK: cir.return
+
+unsigned test_stdc_count_ones_ui(unsigned x) {
+  return stdc_count_ones_ui(x);
+}
+
+// CHECK-LABEL: test_stdc_count_ones_ui
+// CHECK: cir.popcount
+// CHECK: cir.return

>From 08a059d8c8de71c2cbf959cbcab8043ccebf17ff Mon Sep 17 00:00:00 2001
From: Kunal Dubey <[email protected]>
Date: Mon, 10 Aug 2026 18:21:43 +0530
Subject: [PATCH 4/6] [CIR] Lowering __builtin_stdc_bit_width

Added lowering for __builtin_stdc_bit_width by addition of helper
emitStdcBitWidth that does integer type width minus leading zero count.

Added tests for the same.
---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp       | 28 +++++++++++++++++++
 .../CIR/CodeGenBuiltins/builtin-stdc-bit.c    | 24 +++++++++++++++-
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index d27ace31cef78..aef5818084bc4 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -111,6 +111,26 @@ static RValue emitBuiltinBitOpWithFallback(CIRGenFunction 
&cgf,
   return RValue::get(builder.createSelect(loc, isZero, fallbackValue, result));
 }
 
+static RValue emitStdcBitWidth(CIRGenFunction &cfg, const CallExpr *e) {
+  CIRGenBuilderTy &builder = cfg.getBuilder();
+  mlir::Location loc = cfg.getLoc(e->getSourceRange());
+
+  mlir::Value arg = cfg.emitScalarExpr(e->getArg(0));
+  auto argTy = mlir::cast<cir::IntType>(arg.getType());
+
+  mlir::Value lz =
+      createBuiltinBitOp<cir::BitClzOp>(cfg, e, arg, /*poisonZero=*/false);
+
+  mlir::Value width = builder.getConstInt(loc, argTy, argTy.getWidth());
+  mlir::Value result = builder.createSub(loc, width, lz);
+
+  mlir::Type resultTy = cfg.convertType(e->getType());
+  if (result.getType() != resultTy)
+    result = builder.createIntCast(result, resultTy);
+
+  return RValue::get(result);
+}
+
 /// Emit the conversions required to turn the given value into an
 /// integer of the given size.
 static mlir::Value emitToInt(CIRGenFunction &cgf, mlir::Value v, QualType t,
@@ -1263,6 +1283,14 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BI__builtin_stdc_leading_zeros:
     return emitBuiltinBitOp<cir::BitClzOp>(*this, e, /*poisonZero=*/false);
 
+  case Builtin::BIstdc_bit_width_uc:
+  case Builtin::BIstdc_bit_width_us:
+  case Builtin::BIstdc_bit_width_ui:
+  case Builtin::BIstdc_bit_width_ul:
+  case Builtin::BIstdc_bit_width_ull:
+  case Builtin::BI__builtin_stdc_bit_width:
+    return emitStdcBitWidth(*this, e);
+
   case Builtin::BIstdc_count_ones_uc:
   case Builtin::BIstdc_count_ones_us:
   case Builtin::BIstdc_count_ones_ui:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
index e9d5b25a10f0e..56632e70761ef 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o 
- | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 
-I%S/../../CodeGen/Inputs -fclangir -emit-cir %s -o - | FileCheck %s
+
+#include <stdbit.h>
 
 void test_stdc_trailing_zeros(unsigned long long x) {
   int cnt = __builtin_stdc_trailing_zeros(x);
@@ -43,3 +45,23 @@ unsigned test_stdc_count_ones_ui(unsigned x) {
 // CHECK-LABEL: test_stdc_count_ones_ui
 // CHECK: cir.popcount
 // CHECK: cir.return
+
+unsigned test_stdc_bit_width(unsigned x) {
+  return __builtin_stdc_bit_width(x);
+}
+
+// CHECK-LABEL: test_stdc_bit_width
+// CHECK: cir.clz
+// CHECK-NOT: poison_zero
+// CHECK: cir.sub
+// CHECK: cir.return
+
+unsigned test_stdc_bit_width_ui(unsigned x) {
+  return stdc_bit_width_ui(x);
+}
+
+// CHECK-LABEL: test_stdc_bit_width_ui
+// CHECK: cir.clz
+// CHECK-NOT: poison_zero
+// CHECK: cir.sub
+// CHECK: cir.return

>From 39676dc2974588426269931c4402d6dafce1be06 Mon Sep 17 00:00:00 2001
From: Kunal Dubey <[email protected]>
Date: Tue, 11 Aug 2026 12:57:31 +0530
Subject: [PATCH 5/6] [CIR] Lower __builting_stdc_* functions

Added helpers and fixed shape for the previously added builtin functions
to mirror the same as the Classic CodeGen code style and OGCG test style
for the tests.
---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp       |  56 +++-
 .../test/CIR/CodeGenBuiltins/Inputs/stdbit.h  |  16 ++
 .../CIR/CodeGenBuiltins/builtin-stdc-bit.c    | 271 +++++++++++++++---
 3 files changed, 286 insertions(+), 57 deletions(-)
 create mode 100644 clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index aef5818084bc4..98f18111945b6 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -111,20 +111,38 @@ static RValue emitBuiltinBitOpWithFallback(CIRGenFunction 
&cgf,
   return RValue::get(builder.createSelect(loc, isZero, fallbackValue, result));
 }
 
-static RValue emitStdcBitWidth(CIRGenFunction &cfg, const CallExpr *e) {
-  CIRGenBuilderTy &builder = cfg.getBuilder();
-  mlir::Location loc = cfg.getLoc(e->getSourceRange());
+// stdc_{leading,trailing}_zeros and stdc_count_ones: counts bits using clz,
+// ctz, or popcount. InvertArg flips the input to count the opposite bit value.
+template <typename Op, typename... Args>
+static RValue emitStdcCountOp(CIRGenFunction &cgf, const CallExpr *e,
+                              bool invertArg, Args... args) {
+  CIRGenBuilderTy &builder = cgf.getBuilder();
+  mlir::Location loc = cgf.getLoc(e->getSourceRange());
+  mlir::Value arg = cgf.emitScalarExpr(e->getArg(0));
+  mlir::Value actualArg = invertArg ? builder.createNot(loc, arg) : arg;
+  mlir::Value result = Op::create(builder, loc, actualArg, 
args...).getResult();
 
-  mlir::Value arg = cfg.emitScalarExpr(e->getArg(0));
-  auto argTy = mlir::cast<cir::IntType>(arg.getType());
+  mlir::Type resultTy = cgf.convertType(e->getType());
+  if (result.getType() != resultTy)
+    result = builder.createIntCast(result, resultTy);
+  return RValue::get(result);
+}
 
-  mlir::Value lz =
-      createBuiltinBitOp<cir::BitClzOp>(cfg, e, arg, /*poisonZero=*/false);
+// stdc_count_zeros (BitWidth - popcount) and stdc_bit_width (BitWidth - clz).
+// The subtract is performed in the argument type and cast once at the end.
+template <typename Op, typename... Args>
+static RValue emitStdcBitWidthMinus(CIRGenFunction &cgf, const CallExpr *e,
+                                    Args... args) {
+  CIRGenBuilderTy &builder = cgf.getBuilder();
+  mlir::Location loc = cgf.getLoc(e->getSourceRange());
+  mlir::Value arg = cgf.emitScalarExpr(e->getArg(0));
+  auto argTy = mlir::cast<cir::IntType>(arg.getType());
 
+  mlir::Value cnt = Op::create(builder, loc, arg, args...).getResult();
   mlir::Value width = builder.getConstInt(loc, argTy, argTy.getWidth());
-  mlir::Value result = builder.createSub(loc, width, lz);
+  mlir::Value result = builder.createSub(loc, width, cnt);
 
-  mlir::Type resultTy = cfg.convertType(e->getType());
+  mlir::Type resultTy = cgf.convertType(e->getType());
   if (result.getType() != resultTy)
     result = builder.createIntCast(result, resultTy);
 
@@ -1273,7 +1291,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BIstdc_trailing_zeros_ul:
   case Builtin::BIstdc_trailing_zeros_ull:
   case Builtin::BI__builtin_stdc_trailing_zeros:
-    return emitBuiltinBitOp<cir::BitCtzOp>(*this, e, /*poisonZero=*/false);
+    return emitStdcCountOp<cir::BitCtzOp>(*this, e, /*invertArg=*/false,
+                                          /*poisonZero=*/false);
 
   case Builtin::BIstdc_leading_zeros_uc:
   case Builtin::BIstdc_leading_zeros_us:
@@ -1281,7 +1300,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BIstdc_leading_zeros_ul:
   case Builtin::BIstdc_leading_zeros_ull:
   case Builtin::BI__builtin_stdc_leading_zeros:
-    return emitBuiltinBitOp<cir::BitClzOp>(*this, e, /*poisonZero=*/false);
+    return emitStdcCountOp<cir::BitClzOp>(*this, e, /*invertArg=*/false,
+                                          /*poisonZero=*/false);
 
   case Builtin::BIstdc_bit_width_uc:
   case Builtin::BIstdc_bit_width_us:
@@ -1289,7 +1309,16 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BIstdc_bit_width_ul:
   case Builtin::BIstdc_bit_width_ull:
   case Builtin::BI__builtin_stdc_bit_width:
-    return emitStdcBitWidth(*this, e);
+    return emitStdcBitWidthMinus<cir::BitClzOp>(*this, e,
+                                                /*poisonZero=*/false);
+
+  case Builtin::BIstdc_count_zeros_uc:
+  case Builtin::BIstdc_count_zeros_us:
+  case Builtin::BIstdc_count_zeros_ui:
+  case Builtin::BIstdc_count_zeros_ul:
+  case Builtin::BIstdc_count_zeros_ull:
+  case Builtin::BI__builtin_stdc_count_zeros:
+    return emitStdcBitWidthMinus<cir::BitPopcountOp>(*this, e);
 
   case Builtin::BIstdc_count_ones_uc:
   case Builtin::BIstdc_count_ones_us:
@@ -1297,7 +1326,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BIstdc_count_ones_ul:
   case Builtin::BIstdc_count_ones_ull:
   case Builtin::BI__builtin_stdc_count_ones:
-    return emitBuiltinBitOp<cir::BitPopcountOp>(*this, e);
+    return emitStdcCountOp<cir::BitPopcountOp>(*this, e,
+                                               /*invertArg=*/false);
 
   case Builtin::BI__builtin_clzs:
   case Builtin::BI__builtin_clz:
diff --git a/clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h 
b/clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h
new file mode 100644
index 0000000000000..e61d333b221bd
--- /dev/null
+++ b/clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h
@@ -0,0 +1,16 @@
+#ifndef LLVM_CLANG_TEST_CIR_STDBIT_H
+#define LLVM_CLANG_TEST_CIR_STDBIT_H
+
+unsigned int stdc_count_zeros_uc(unsigned char);
+unsigned int stdc_count_zeros_us(unsigned short);
+unsigned int stdc_count_zeros_ui(unsigned int);
+unsigned int stdc_count_zeros_ul(unsigned long);
+unsigned int stdc_count_zeros_ull(unsigned long long);
+
+unsigned int stdc_bit_width_uc(unsigned char);
+unsigned int stdc_bit_width_us(unsigned short);
+unsigned int stdc_bit_width_ui(unsigned int);
+unsigned int stdc_bit_width_ul(unsigned long);
+unsigned int stdc_bit_width_ull(unsigned long long);
+
+#endif
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
index 56632e70761ef..ffb9f43f58a31 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
@@ -1,67 +1,250 @@
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 
-I%S/../../CodeGen/Inputs -fclangir -emit-cir %s -o - | FileCheck %s
+// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux-gnu -std=c23 
-fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
+// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux-gnu -std=c23 
-fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
+// RUN: %clang_cc1 -ffreestanding -triple x86_64-unknown-linux-gnu -std=c23 
-emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c23 -isystem 
%S/Inputs -DTEST_LIB_SPELLINGS -fclangir -emit-cir %s -o %t-lib.cir
+// RUN: FileCheck --input-file=%t-lib.cir %s --check-prefix=LIB-CIR
 
+#ifdef TEST_LIB_SPELLINGS
 #include <stdbit.h>
+#endif
 
-void test_stdc_trailing_zeros(unsigned long long x) {
-  int cnt = __builtin_stdc_trailing_zeros(x);
-  (void)cnt;
+#ifndef TEST_LIB_SPELLINGS
+
+void test_stdc_trailing_zeros(unsigned char uc, unsigned short us,
+                              unsigned int ui, unsigned long ul,
+                              unsigned long long ull) {
+  volatile unsigned int r;
+  r = __builtin_stdc_trailing_zeros(uc);
+  r = __builtin_stdc_trailing_zeros(us);
+  r = __builtin_stdc_trailing_zeros(ui);
+  r = __builtin_stdc_trailing_zeros(ul);
+  r = __builtin_stdc_trailing_zeros(ull);
 }
 
-// CHECK-LABEL: test_stdc_trailing_zeros
-// CHECK: cir.ctz
-// CHECK-NOT: poison_zero
-// CHECK: cir.return
+// CIR-LABEL: @test_stdc_trailing_zeros(
+// CIR: cir.ctz %{{.+}} : !u8i
+// CIR: cir.ctz %{{.+}} : !u16i
+// CIR: cir.ctz %{{.+}} : !u32i
+// CIR: cir.ctz %{{.+}} : !u64i
+// CIR: cir.ctz %{{.+}} : !u64i
+// LLVM-LABEL: @test_stdc_trailing_zeros(
+// LLVM: call i8 @llvm.cttz.i8(i8 %{{.*}}, i1 false)
+// LLVM: call i16 @llvm.cttz.i16(i16 %{{.*}}, i1 false)
+// LLVM: call i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false)
+// LLVM: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
+// LLVM: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
+// OGCG-LABEL: @test_stdc_trailing_zeros(
+// OGCG: call i8 @llvm.cttz.i8(i8 %{{.*}}, i1 false)
+// OGCG: call i16 @llvm.cttz.i16(i16 %{{.*}}, i1 false)
+// OGCG: call i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false)
+// OGCG: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
+// OGCG: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
 
-unsigned test_stdc_leading_zeros(unsigned x) {
-  return __builtin_stdc_leading_zeros(x);
+unsigned int test_stdc_leading_zeros(unsigned char uc, unsigned short us,
+                                     unsigned int ui, unsigned long ul,
+                                     unsigned long long ull) {
+  volatile unsigned int r;
+  r = __builtin_stdc_leading_zeros(uc);
+  r = __builtin_stdc_leading_zeros(us);
+  r = __builtin_stdc_leading_zeros(ui);
+  r = __builtin_stdc_leading_zeros(ul);
+  r = __builtin_stdc_leading_zeros(ull);
+  return r;
 }
 
-// CHECK-LABEL: test_stdc_leading_zeros
-// CHECK: cir.clz
-// CHECK-NOT: poison_zero
-// CHECK: cir.return
+// CIR-LABEL: @test_stdc_leading_zeros(
+// CIR: cir.clz %{{.+}} : !u8i
+// CIR: cir.clz %{{.+}} : !u16i
+// CIR: cir.clz %{{.+}} : !u32i
+// CIR: cir.clz %{{.+}} : !u64i
+// CIR: cir.clz %{{.+}} : !u64i
+// LLVM-LABEL: @test_stdc_leading_zeros(
+// LLVM: call i8 @llvm.ctlz.i8(i8 %{{.*}}, i1 false)
+// LLVM: call i16 @llvm.ctlz.i16(i16 %{{.*}}, i1 false)
+// LLVM: call i32 @llvm.ctlz.i32(i32 %{{.*}}, i1 false)
+// LLVM: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// LLVM: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// OGCG-LABEL: @test_stdc_leading_zeros(
+// OGCG: call i8 @llvm.ctlz.i8(i8 %{{.*}}, i1 false)
+// OGCG: call i16 @llvm.ctlz.i16(i16 %{{.*}}, i1 false)
+// OGCG: call i32 @llvm.ctlz.i32(i32 %{{.*}}, i1 false)
+// OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
 
-unsigned test_stdc_leading_zeros_ui(unsigned x) {
-  return stdc_leading_zeros_ui(x);
+unsigned int test_stdc_count_ones(unsigned char uc, unsigned short us,
+                                  unsigned int ui, unsigned long ul,
+                                  unsigned long long ull) {
+  volatile unsigned int r;
+  r = __builtin_stdc_count_ones(uc);
+  r = __builtin_stdc_count_ones(us);
+  r = __builtin_stdc_count_ones(ui);
+  r = __builtin_stdc_count_ones(ul);
+  r = __builtin_stdc_count_ones(ull);
+  return r;
 }
 
-// CHECK-LABEL: test_stdc_leading_zeros_ui
-// CHECK: cir.clz
-// CHECK-NOT: poison_zero
-// CHECK: cir.return
+// CIR-LABEL: @test_stdc_count_ones(
+// CIR: cir.popcount %{{.+}} : !u8i
+// CIR: cir.popcount %{{.+}} : !u16i
+// CIR: cir.popcount %{{.+}} : !u32i
+// CIR: cir.popcount %{{.+}} : !u64i
+// CIR: cir.popcount %{{.+}} : !u64i
+// LLVM-LABEL: @test_stdc_count_ones(
+// LLVM: call i8 @llvm.ctpop.i8(i8 %{{.*}})
+// LLVM: call i16 @llvm.ctpop.i16(i16 %{{.*}})
+// LLVM: call i32 @llvm.ctpop.i32(i32 %{{.*}})
+// LLVM: call i64 @llvm.ctpop.i64(i64 %{{.*}})
+// LLVM: call i64 @llvm.ctpop.i64(i64 %{{.*}})
+// OGCG-LABEL: @test_stdc_count_ones(
+// OGCG: call i8 @llvm.ctpop.i8(i8 %{{.*}})
+// OGCG: call i16 @llvm.ctpop.i16(i16 %{{.*}})
+// OGCG: call i32 @llvm.ctpop.i32(i32 %{{.*}})
+// OGCG: call i64 @llvm.ctpop.i64(i64 %{{.*}})
+// OGCG: call i64 @llvm.ctpop.i64(i64 %{{.*}})
 
-unsigned test_stdc_count_ones(unsigned x) {
-  return __builtin_stdc_count_ones(x);
+unsigned int test_stdc_count_zeros(unsigned char uc, unsigned short us,
+                                   unsigned int ui, unsigned long ul,
+                                   unsigned long long ull) {
+  volatile unsigned int r;
+  r = __builtin_stdc_count_zeros(uc);
+  r = __builtin_stdc_count_zeros(us);
+  r = __builtin_stdc_count_zeros(ui);
+  r = __builtin_stdc_count_zeros(ul);
+  r = __builtin_stdc_count_zeros(ull);
+  return r;
 }
 
-// CHECK-LABEL: test_stdc_count_ones
-// CHECK: cir.popcount
-// CHECK: cir.return
+// CIR-LABEL: @test_stdc_count_zeros(
+// CIR: cir.popcount %{{.+}} : !u8i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u8i
+// CIR: cir.popcount %{{.+}} : !u16i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u16i
+// CIR: cir.popcount %{{.+}} : !u32i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u32i
+// CIR: cir.popcount %{{.+}} : !u64i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
+// CIR: cir.popcount %{{.+}} : !u64i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
+// LLVM-LABEL: @test_stdc_count_zeros(
+// LLVM: call i8 @llvm.ctpop.i8(i8 %{{.*}})
+// LLVM: sub i8 8, %{{.*}}
+// LLVM: call i16 @llvm.ctpop.i16(i16 %{{.*}})
+// LLVM: sub i16 16, %{{.*}}
+// LLVM: call i32 @llvm.ctpop.i32(i32 %{{.*}})
+// LLVM: sub i32 32, %{{.*}}
+// LLVM: call i64 @llvm.ctpop.i64(i64 %{{.*}})
+// LLVM: sub i64 64, %{{.*}}
+// LLVM: call i64 @llvm.ctpop.i64(i64 %{{.*}})
+// LLVM: sub i64 64, %{{.*}}
+// OGCG-LABEL: @test_stdc_count_zeros(
+// OGCG: call i8 @llvm.ctpop.i8(i8 %{{.*}})
+// OGCG: sub i8 8, %{{.*}}
+// OGCG: call i16 @llvm.ctpop.i16(i16 %{{.*}})
+// OGCG: sub i16 16, %{{.*}}
+// OGCG: call i32 @llvm.ctpop.i32(i32 %{{.*}})
+// OGCG: sub i32 32, %{{.*}}
+// OGCG: call i64 @llvm.ctpop.i64(i64 %{{.*}})
+// OGCG: sub i64 64, %{{.*}}
+// OGCG: call i64 @llvm.ctpop.i64(i64 %{{.*}})
+// OGCG: sub i64 64, %{{.*}}
 
-unsigned test_stdc_count_ones_ui(unsigned x) {
-  return stdc_count_ones_ui(x);
+unsigned int test_stdc_bit_width(unsigned char uc, unsigned short us,
+                                 unsigned int ui, unsigned long ul,
+                                 unsigned long long ull) {
+  volatile unsigned int r;
+  r = __builtin_stdc_bit_width(uc);
+  r = __builtin_stdc_bit_width(us);
+  r = __builtin_stdc_bit_width(ui);
+  r = __builtin_stdc_bit_width(ul);
+  r = __builtin_stdc_bit_width(ull);
+  return r;
 }
 
-// CHECK-LABEL: test_stdc_count_ones_ui
-// CHECK: cir.popcount
-// CHECK: cir.return
+// CIR-LABEL: @test_stdc_bit_width(
+// CIR: cir.clz %{{.+}} : !u8i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u8i
+// CIR: cir.clz %{{.+}} : !u16i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u16i
+// CIR: cir.clz %{{.+}} : !u32i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u32i
+// CIR: cir.clz %{{.+}} : !u64i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
+// CIR: cir.clz %{{.+}} : !u64i
+// CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
+// LLVM-LABEL: @test_stdc_bit_width(
+// LLVM: call i8 @llvm.ctlz.i8(i8 %{{.*}}, i1 false)
+// LLVM: sub i8 8, %{{.*}}
+// LLVM: call i16 @llvm.ctlz.i16(i16 %{{.*}}, i1 false)
+// LLVM: sub i16 16, %{{.*}}
+// LLVM: call i32 @llvm.ctlz.i32(i32 %{{.*}}, i1 false)
+// LLVM: sub i32 32, %{{.*}}
+// LLVM: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// LLVM: sub i64 64, %{{.*}}
+// LLVM: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// LLVM: sub i64 64, %{{.*}}
+// OGCG-LABEL: @test_stdc_bit_width(
+// OGCG: call i8 @llvm.ctlz.i8(i8 %{{.*}}, i1 false)
+// OGCG: sub i8 8, %{{.*}}
+// OGCG: call i16 @llvm.ctlz.i16(i16 %{{.*}}, i1 false)
+// OGCG: sub i16 16, %{{.*}}
+// OGCG: call i32 @llvm.ctlz.i32(i32 %{{.*}}, i1 false)
+// OGCG: sub i32 32, %{{.*}}
+// OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// OGCG: sub i64 64, %{{.*}}
+// OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// OGCG: sub i64 64, %{{.*}}
+
+#else
 
-unsigned test_stdc_bit_width(unsigned x) {
-  return __builtin_stdc_bit_width(x);
+unsigned int test_stdc_count_zeros_lib(unsigned char uc, unsigned short us,
+                                       unsigned int ui, unsigned long ul,
+                                       unsigned long long ull) {
+  volatile unsigned int r;
+  r = stdc_count_zeros_uc(uc);
+  r = stdc_count_zeros_us(us);
+  r = stdc_count_zeros_ui(ui);
+  r = stdc_count_zeros_ul(ul);
+  r = stdc_count_zeros_ull(ull);
+  return r;
 }
 
-// CHECK-LABEL: test_stdc_bit_width
-// CHECK: cir.clz
-// CHECK-NOT: poison_zero
-// CHECK: cir.sub
-// CHECK: cir.return
+// LIB-CIR-LABEL: @test_stdc_count_zeros_lib(
+// LIB-CIR: cir.popcount %{{.+}} : !u8i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u8i
+// LIB-CIR: cir.popcount %{{.+}} : !u16i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u16i
+// LIB-CIR: cir.popcount %{{.+}} : !u32i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u32i
+// LIB-CIR: cir.popcount %{{.+}} : !u64i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
+// LIB-CIR: cir.popcount %{{.+}} : !u64i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
 
-unsigned test_stdc_bit_width_ui(unsigned x) {
-  return stdc_bit_width_ui(x);
+unsigned int test_stdc_bit_width_lib(unsigned char uc, unsigned short us,
+                                     unsigned int ui, unsigned long ul,
+                                     unsigned long long ull) {
+  volatile unsigned int r;
+  r = stdc_bit_width_uc(uc);
+  r = stdc_bit_width_us(us);
+  r = stdc_bit_width_ui(ui);
+  r = stdc_bit_width_ul(ul);
+  r = stdc_bit_width_ull(ull);
+  return r;
 }
 
-// CHECK-LABEL: test_stdc_bit_width_ui
-// CHECK: cir.clz
-// CHECK-NOT: poison_zero
-// CHECK: cir.sub
-// CHECK: cir.return
+// LIB-CIR-LABEL: @test_stdc_bit_width_lib(
+// LIB-CIR: cir.clz %{{.+}} : !u8i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u8i
+// LIB-CIR: cir.clz %{{.+}} : !u16i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u16i
+// LIB-CIR: cir.clz %{{.+}} : !u32i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u32i
+// LIB-CIR: cir.clz %{{.+}} : !u64i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
+// LIB-CIR: cir.clz %{{.+}} : !u64i
+// LIB-CIR: cir.sub %{{.+}}, %{{.+}} : !u64i
+
+#endif

>From 56cd327d466edbf03606d1e80d2988e666b97e61 Mon Sep 17 00:00:00 2001
From: Kunal Dubey <[email protected]>
Date: Tue, 11 Aug 2026 15:46:50 +0530
Subject: [PATCH 6/6] [CIR] Lowering for __builtin_stdc_trailing and
 _leading_ones

Added lowerin for the __builting_stdc_trailing and
__builin_stdc_leading_ones by lowering to by inverted BitCtzOp and
inverted BitCtzOp respectively, matching the C23 stdbit semantics.

Added errorBuiltinNYI for the other Builtins that are not implemented.

Added OGCG style test suite for the same.
---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp       |  47 +++++-
 .../test/CIR/CodeGenBuiltins/Inputs/stdbit.h  |  12 ++
 .../CIR/CodeGenBuiltins/builtin-stdc-bit.c    | 139 ++++++++++++++++++
 3 files changed, 196 insertions(+), 2 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index 98f18111945b6..43d62b964944d 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -111,8 +111,9 @@ static RValue emitBuiltinBitOpWithFallback(CIRGenFunction 
&cgf,
   return RValue::get(builder.createSelect(loc, isZero, fallbackValue, result));
 }
 
-// stdc_{leading,trailing}_zeros and stdc_count_ones: counts bits using clz,
-// ctz, or popcount. InvertArg flips the input to count the opposite bit value.
+// stdc_{leading,trailing}_{zeros,ones} and stdc_count_ones: counts bits using
+// clz, ctz, or popcount. InvertArg flips the input to count the opposite bit
+// value.
 template <typename Op, typename... Args>
 static RValue emitStdcCountOp(CIRGenFunction &cgf, const CallExpr *e,
                               bool invertArg, Args... args) {
@@ -1303,6 +1304,24 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
     return emitStdcCountOp<cir::BitClzOp>(*this, e, /*invertArg=*/false,
                                           /*poisonZero=*/false);
 
+  case Builtin::BIstdc_trailing_ones_uc:
+  case Builtin::BIstdc_trailing_ones_us:
+  case Builtin::BIstdc_trailing_ones_ui:
+  case Builtin::BIstdc_trailing_ones_ul:
+  case Builtin::BIstdc_trailing_ones_ull:
+  case Builtin::BI__builtin_stdc_trailing_ones:
+    return emitStdcCountOp<cir::BitCtzOp>(*this, e, /*invertArg=*/true,
+                                          /*poisonZero=*/false);
+
+  case Builtin::BIstdc_leading_ones_uc:
+  case Builtin::BIstdc_leading_ones_us:
+  case Builtin::BIstdc_leading_ones_ui:
+  case Builtin::BIstdc_leading_ones_ul:
+  case Builtin::BIstdc_leading_ones_ull:
+  case Builtin::BI__builtin_stdc_leading_ones:
+    return emitStdcCountOp<cir::BitClzOp>(*this, e, /*invertArg=*/true,
+                                          /*poisonZero=*/false);
+
   case Builtin::BIstdc_bit_width_uc:
   case Builtin::BIstdc_bit_width_us:
   case Builtin::BIstdc_bit_width_ui:
@@ -1329,6 +1348,30 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
     return emitStdcCountOp<cir::BitPopcountOp>(*this, e,
                                                /*invertArg=*/false);
 
+  case Builtin::BIstdc_has_single_bit_uc:
+  case Builtin::BIstdc_has_single_bit_us:
+  case Builtin::BIstdc_has_single_bit_ui:
+  case Builtin::BIstdc_has_single_bit_ul:
+  case Builtin::BIstdc_has_single_bit_ull:
+  case Builtin::BI__builtin_stdc_has_single_bit:
+    return errorBuiltinNYI(*this, e, builtinID);
+
+  case Builtin::BIstdc_bit_ceil_uc:
+  case Builtin::BIstdc_bit_ceil_us:
+  case Builtin::BIstdc_bit_ceil_ui:
+  case Builtin::BIstdc_bit_ceil_ul:
+  case Builtin::BIstdc_bit_ceil_ull:
+  case Builtin::BI__builtin_stdc_bit_ceil:
+    return errorBuiltinNYI(*this, e, builtinID);
+
+  case Builtin::BIstdc_bit_floor_uc:
+  case Builtin::BIstdc_bit_floor_us:
+  case Builtin::BIstdc_bit_floor_ui:
+  case Builtin::BIstdc_bit_floor_ul:
+  case Builtin::BIstdc_bit_floor_ull:
+  case Builtin::BI__builtin_stdc_bit_floor:
+    return errorBuiltinNYI(*this, e, builtinID);
+
   case Builtin::BI__builtin_clzs:
   case Builtin::BI__builtin_clz:
   case Builtin::BI__builtin_clzl:
diff --git a/clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h 
b/clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h
index e61d333b221bd..f4ee39e486558 100644
--- a/clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h
+++ b/clang/test/CIR/CodeGenBuiltins/Inputs/stdbit.h
@@ -1,6 +1,18 @@
 #ifndef LLVM_CLANG_TEST_CIR_STDBIT_H
 #define LLVM_CLANG_TEST_CIR_STDBIT_H
 
+unsigned int stdc_leading_ones_uc(unsigned char);
+unsigned int stdc_leading_ones_us(unsigned short);
+unsigned int stdc_leading_ones_ui(unsigned int);
+unsigned int stdc_leading_ones_ul(unsigned long);
+unsigned int stdc_leading_ones_ull(unsigned long long);
+
+unsigned int stdc_trailing_ones_uc(unsigned char);
+unsigned int stdc_trailing_ones_us(unsigned short);
+unsigned int stdc_trailing_ones_ui(unsigned int);
+unsigned int stdc_trailing_ones_ul(unsigned long);
+unsigned int stdc_trailing_ones_ull(unsigned long long);
+
 unsigned int stdc_count_zeros_uc(unsigned char);
 unsigned int stdc_count_zeros_us(unsigned short);
 unsigned int stdc_count_zeros_ui(unsigned int);
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
index ffb9f43f58a31..b7024af2475e5 100644
--- a/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-stdc-bit.c
@@ -43,6 +43,51 @@ void test_stdc_trailing_zeros(unsigned char uc, unsigned 
short us,
 // OGCG: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
 // OGCG: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
 
+void test_stdc_trailing_ones(unsigned char uc, unsigned short us,
+                             unsigned int ui, unsigned long ul,
+                             unsigned long long ull) {
+  volatile unsigned int r;
+  r = __builtin_stdc_trailing_ones(uc);
+  r = __builtin_stdc_trailing_ones(us);
+  r = __builtin_stdc_trailing_ones(ui);
+  r = __builtin_stdc_trailing_ones(ul);
+  r = __builtin_stdc_trailing_ones(ull);
+}
+
+// CIR-LABEL: @test_stdc_trailing_ones(
+// CIR: cir.not %{{.+}} : !u8i
+// CIR: cir.ctz %{{.+}} : !u8i
+// CIR: cir.not %{{.+}} : !u16i
+// CIR: cir.ctz %{{.+}} : !u16i
+// CIR: cir.not %{{.+}} : !u32i
+// CIR: cir.ctz %{{.+}} : !u32i
+// CIR: cir.not %{{.+}} : !u64i
+// CIR: cir.ctz %{{.+}} : !u64i
+// CIR: cir.not %{{.+}} : !u64i
+// CIR: cir.ctz %{{.+}} : !u64i
+// LLVM-LABEL: @test_stdc_trailing_ones(
+// LLVM: xor i8 %{{.*}}, -1
+// LLVM: call i8 @llvm.cttz.i8(i8 %{{.*}}, i1 false)
+// LLVM: xor i16 %{{.*}}, -1
+// LLVM: call i16 @llvm.cttz.i16(i16 %{{.*}}, i1 false)
+// LLVM: xor i32 %{{.*}}, -1
+// LLVM: call i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false)
+// LLVM: xor i64 %{{.*}}, -1
+// LLVM: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
+// LLVM: xor i64 %{{.*}}, -1
+// LLVM: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
+// OGCG-LABEL: @test_stdc_trailing_ones(
+// OGCG: xor i8 %{{.*}}, -1
+// OGCG: call i8 @llvm.cttz.i8(i8 %{{.*}}, i1 false)
+// OGCG: xor i16 %{{.*}}, -1
+// OGCG: call i16 @llvm.cttz.i16(i16 %{{.*}}, i1 false)
+// OGCG: xor i32 %{{.*}}, -1
+// OGCG: call i32 @llvm.cttz.i32(i32 %{{.*}}, i1 false)
+// OGCG: xor i64 %{{.*}}, -1
+// OGCG: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
+// OGCG: xor i64 %{{.*}}, -1
+// OGCG: call i64 @llvm.cttz.i64(i64 %{{.*}}, i1 false)
+
 unsigned int test_stdc_leading_zeros(unsigned char uc, unsigned short us,
                                      unsigned int ui, unsigned long ul,
                                      unsigned long long ull) {
@@ -74,6 +119,52 @@ unsigned int test_stdc_leading_zeros(unsigned char uc, 
unsigned short us,
 // OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
 // OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
 
+unsigned int test_stdc_leading_ones(unsigned char uc, unsigned short us,
+                                    unsigned int ui, unsigned long ul,
+                                    unsigned long long ull) {
+  volatile unsigned int r;
+  r = __builtin_stdc_leading_ones(uc);
+  r = __builtin_stdc_leading_ones(us);
+  r = __builtin_stdc_leading_ones(ui);
+  r = __builtin_stdc_leading_ones(ul);
+  r = __builtin_stdc_leading_ones(ull);
+  return r;
+}
+
+// CIR-LABEL: @test_stdc_leading_ones(
+// CIR: cir.not %{{.+}} : !u8i
+// CIR: cir.clz %{{.+}} : !u8i
+// CIR: cir.not %{{.+}} : !u16i
+// CIR: cir.clz %{{.+}} : !u16i
+// CIR: cir.not %{{.+}} : !u32i
+// CIR: cir.clz %{{.+}} : !u32i
+// CIR: cir.not %{{.+}} : !u64i
+// CIR: cir.clz %{{.+}} : !u64i
+// CIR: cir.not %{{.+}} : !u64i
+// CIR: cir.clz %{{.+}} : !u64i
+// LLVM-LABEL: @test_stdc_leading_ones(
+// LLVM: xor i8 %{{.*}}, -1
+// LLVM: call i8 @llvm.ctlz.i8(i8 %{{.*}}, i1 false)
+// LLVM: xor i16 %{{.*}}, -1
+// LLVM: call i16 @llvm.ctlz.i16(i16 %{{.*}}, i1 false)
+// LLVM: xor i32 %{{.*}}, -1
+// LLVM: call i32 @llvm.ctlz.i32(i32 %{{.*}}, i1 false)
+// LLVM: xor i64 %{{.*}}, -1
+// LLVM: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// LLVM: xor i64 %{{.*}}, -1
+// LLVM: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// OGCG-LABEL: @test_stdc_leading_ones(
+// OGCG: xor i8 %{{.*}}, -1
+// OGCG: call i8 @llvm.ctlz.i8(i8 %{{.*}}, i1 false)
+// OGCG: xor i16 %{{.*}}, -1
+// OGCG: call i16 @llvm.ctlz.i16(i16 %{{.*}}, i1 false)
+// OGCG: xor i32 %{{.*}}, -1
+// OGCG: call i32 @llvm.ctlz.i32(i32 %{{.*}}, i1 false)
+// OGCG: xor i64 %{{.*}}, -1
+// OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+// OGCG: xor i64 %{{.*}}, -1
+// OGCG: call i64 @llvm.ctlz.i64(i64 %{{.*}}, i1 false)
+
 unsigned int test_stdc_count_ones(unsigned char uc, unsigned short us,
                                   unsigned int ui, unsigned long ul,
                                   unsigned long long ull) {
@@ -199,6 +290,54 @@ unsigned int test_stdc_bit_width(unsigned char uc, 
unsigned short us,
 
 #else
 
+unsigned int test_stdc_trailing_ones_lib(unsigned char uc, unsigned short us,
+                                         unsigned int ui, unsigned long ul,
+                                         unsigned long long ull) {
+  volatile unsigned int r;
+  r = stdc_trailing_ones_uc(uc);
+  r = stdc_trailing_ones_us(us);
+  r = stdc_trailing_ones_ui(ui);
+  r = stdc_trailing_ones_ul(ul);
+  r = stdc_trailing_ones_ull(ull);
+  return r;
+}
+
+// LIB-CIR-LABEL: @test_stdc_trailing_ones_lib(
+// LIB-CIR: cir.not %{{.+}} : !u8i
+// LIB-CIR: cir.ctz %{{.+}} : !u8i
+// LIB-CIR: cir.not %{{.+}} : !u16i
+// LIB-CIR: cir.ctz %{{.+}} : !u16i
+// LIB-CIR: cir.not %{{.+}} : !u32i
+// LIB-CIR: cir.ctz %{{.+}} : !u32i
+// LIB-CIR: cir.not %{{.+}} : !u64i
+// LIB-CIR: cir.ctz %{{.+}} : !u64i
+// LIB-CIR: cir.not %{{.+}} : !u64i
+// LIB-CIR: cir.ctz %{{.+}} : !u64i
+
+unsigned int test_stdc_leading_ones_lib(unsigned char uc, unsigned short us,
+                                        unsigned int ui, unsigned long ul,
+                                        unsigned long long ull) {
+  volatile unsigned int r;
+  r = stdc_leading_ones_uc(uc);
+  r = stdc_leading_ones_us(us);
+  r = stdc_leading_ones_ui(ui);
+  r = stdc_leading_ones_ul(ul);
+  r = stdc_leading_ones_ull(ull);
+  return r;
+}
+
+// LIB-CIR-LABEL: @test_stdc_leading_ones_lib(
+// LIB-CIR: cir.not %{{.+}} : !u8i
+// LIB-CIR: cir.clz %{{.+}} : !u8i
+// LIB-CIR: cir.not %{{.+}} : !u16i
+// LIB-CIR: cir.clz %{{.+}} : !u16i
+// LIB-CIR: cir.not %{{.+}} : !u32i
+// LIB-CIR: cir.clz %{{.+}} : !u32i
+// LIB-CIR: cir.not %{{.+}} : !u64i
+// LIB-CIR: cir.clz %{{.+}} : !u64i
+// LIB-CIR: cir.not %{{.+}} : !u64i
+// LIB-CIR: cir.clz %{{.+}} : !u64i
+
 unsigned int test_stdc_count_zeros_lib(unsigned char uc, unsigned short us,
                                        unsigned int ui, unsigned long ul,
                                        unsigned long long ull) {

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to