[llvm] [ConstantTime][LLVM] Add llvm.ct.select intrinsic with generic SelectionDAG lowering (PR #166702)

Julius Alexandre via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 06:53:34 PDT 2026


================
@@ -0,0 +1,96 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+define i32 @ct_select_true(i32 %x, i32 %y) {
+; CHECK-LABEL: @ct_select_true(
+; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ct.select.i32(i1 true, i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT:    ret i32 [[X]]
+;
+  %r = call i32 @llvm.ct.select.i32(i1 true, i32 %x, i32 %y)
+  ret i32 %r
+}
+
+define i32 @ct_select_false(i32 %x, i32 %y) {
+; CHECK-LABEL: @ct_select_false(
+; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ct.select.i32(i1 false, i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT:    ret i32 [[Y]]
+;
+  %r = call i32 @llvm.ct.select.i32(i1 false, i32 %x, i32 %y)
+  ret i32 %r
+}
+
+define i64 @ct_select_true_i64(i64 %x, i64 %y) {
+; CHECK-LABEL: @ct_select_true_i64(
+; CHECK-NEXT:    [[R:%.*]] = call i64 @llvm.ct.select.i64(i1 true, i64 [[X:%.*]], i64 [[Y:%.*]])
+; CHECK-NEXT:    ret i64 [[X]]
+;
+  %r = call i64 @llvm.ct.select.i64(i1 true, i64 %x, i64 %y)
+  ret i64 %r
+}
+
+define float @ct_select_false_f32(float %x, float %y) {
+; CHECK-LABEL: @ct_select_false_f32(
+; CHECK-NEXT:    [[R:%.*]] = call float @llvm.ct.select.f32(i1 false, float [[X:%.*]], float [[Y:%.*]])
+; CHECK-NEXT:    ret float [[Y]]
+;
+  %r = call float @llvm.ct.select.f32(i1 false, float %x, float %y)
+  ret float %r
+}
+
+define ptr @ct_select_true_ptr(ptr %x, ptr %y) {
+; CHECK-LABEL: @ct_select_true_ptr(
+; CHECK-NEXT:    [[R:%.*]] = call ptr @llvm.ct.select.p0(i1 true, ptr [[X:%.*]], ptr [[Y:%.*]])
+; CHECK-NEXT:    ret ptr [[X]]
+;
+  %r = call ptr @llvm.ct.select.p0(i1 true, ptr %x, ptr %y)
+  ret ptr %r
+}
+
+define <4 x i32> @ct_select_true_v4i32(<4 x i32> %x, <4 x i32> %y) {
+; CHECK-LABEL: @ct_select_true_v4i32(
+; CHECK-NEXT:    [[R:%.*]] = call <4 x i32> @llvm.ct.select.v4i32(i1 true, <4 x i32> [[X:%.*]], <4 x i32> [[Y:%.*]])
+; CHECK-NEXT:    ret <4 x i32> [[X]]
+;
+  %r = call <4 x i32> @llvm.ct.select.v4i32(i1 true, <4 x i32> %x, <4 x i32> %y)
+  ret <4 x i32> %r
+}
+
+define i32 @ct_select_same_arms(i1 %c, i32 %x) {
+; CHECK-LABEL: @ct_select_same_arms(
+; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ct.select.i32(i1 [[C:%.*]], i32 [[X:%.*]], i32 [[X]])
+; CHECK-NEXT:    ret i32 [[X]]
+;
+  %r = call i32 @llvm.ct.select.i32(i1 %c, i32 %x, i32 %x)
+  ret i32 %r
+}
+
+define <4 x i32> @ct_select_same_arms_vec(i1 %c, <4 x i32> %x) {
+; CHECK-LABEL: @ct_select_same_arms_vec(
+; CHECK-NEXT:    [[R:%.*]] = call <4 x i32> @llvm.ct.select.v4i32(i1 [[C:%.*]], <4 x i32> [[X:%.*]], <4 x i32> [[X]])
+; CHECK-NEXT:    ret <4 x i32> [[X]]
+;
+  %r = call <4 x i32> @llvm.ct.select.v4i32(i1 %c, <4 x i32> %x, <4 x i32> %x)
+  ret <4 x i32> %r
+}
+
+; Negative test: must NOT fold when the condition operand is not a constant.
+; InstSimplify deliberately does not run value analyses (known-bits, range) on
+; the condition to derive a fold, so a plain runtime %c keeps the ct.select.
+define i32 @ct_select_runtime_cond_no_fold(i1 %c, i32 %x, i32 %y) {
+; CHECK-LABEL: @ct_select_runtime_cond_no_fold(
+; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ct.select.i32(i1 [[C:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @llvm.ct.select.i32(i1 %c, i32 %x, i32 %y)
+  ret i32 %r
+}
+
+; Negative test: distinct arms with a runtime condition must not fold.
+define i32 @ct_select_distinct_arms_no_fold(i1 %c, i32 %x, i32 %y) {
+; CHECK-LABEL: @ct_select_distinct_arms_no_fold(
+; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.ct.select.i32(i1 [[C:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]])
+; CHECK-NEXT:    ret i32 [[R]]
+;
+  %r = call i32 @llvm.ct.select.i32(i1 %c, i32 %x, i32 %y)
+  ret i32 %r
+}
----------------
wizardengineer wrote:

Added, including scalable vectors and negative cases.

https://github.com/llvm/llvm-project/pull/166702


More information about the llvm-commits mailing list