[llvm] [Analysis] computeKnownBitsFromOperator - add @llvm.clmul knownbits handling and tests (PR #177893)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 25 19:27:47 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Lleu Yang (megakite)
<details>
<summary>Changes</summary>
This adds knownbits handling for `Intrinsic::clmul` inside `computeKnownBitsFromOperator`. Tests are also included.
Closes #<!-- -->177550
---
Full diff: https://github.com/llvm/llvm-project/pull/177893.diff
2 Files Affected:
- (modified) llvm/lib/Analysis/ValueTracking.cpp (+5)
- (added) llvm/test/Analysis/ValueTracking/knownbits-clmul.ll (+79)
``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index b84919d89388d..a36ff1b1bbed5 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2097,6 +2097,11 @@ static void computeKnownBitsFromOperator(const Operator *I,
Known = Known2.unionWith(Known3);
break;
}
+ case Intrinsic::clmul:
+ computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
+ computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
+ Known = KnownBits::clmul(Known, Known2);
+ break;
case Intrinsic::uadd_sat:
computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
diff --git a/llvm/test/Analysis/ValueTracking/knownbits-clmul.ll b/llvm/test/Analysis/ValueTracking/knownbits-clmul.ll
new file mode 100644
index 0000000000000..92c65f18bed05
--- /dev/null
+++ b/llvm/test/Analysis/ValueTracking/knownbits-clmul.ll
@@ -0,0 +1,79 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+define i1 @clmul_zero(i8 %a) nounwind {
+; CHECK-LABEL: @clmul_zero(
+; CHECK-NEXT: ret i1 false
+;
+ %clmul = call i8 @llvm.clmul.i8(i8 %a, i8 0)
+ %r = icmp eq i8 %clmul, 1
+ ret i1 %r
+}
+
+define i1 @clmul_low_bits(i8 %a) nounwind {
+; CHECK-LABEL: @clmul_low_bits(
+; CHECK-NEXT: ret i1 true
+;
+ %clmul = call i8 @llvm.clmul.i8(i8 %a, i8 4)
+ %and = and i8 %clmul, 3
+ %r = icmp eq i8 %and, 0
+ ret i1 %r
+}
+
+define i1 @clmul_low_bits_negative(i8 %a) nounwind {
+; CHECK-LABEL: @clmul_low_bits_negative(
+; CHECK-NEXT: [[CLMUL:%.*]] = call i8 @llvm.clmul.i8(i8 [[A:%.*]], i8 4)
+; CHECK-NEXT: [[AND:%.*]] = and i8 [[CLMUL]], 4
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[AND]], 0
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %clmul = call i8 @llvm.clmul.i8(i8 %a, i8 4)
+ %and = and i8 %clmul, 4
+ %r = icmp eq i8 %and, 0
+ ret i1 %r
+}
+
+define i1 @clmul_high_bits(i8 %a, i8 %b) nounwind {
+; CHECK-LABEL: @clmul_high_bits(
+; CHECK-NEXT: ret i1 true
+;
+ %zext_a = zext i8 %a to i16
+ %zext_b = zext i8 %b to i16
+ %clmul = call i16 @llvm.clmul.i16(i16 %zext_a, i16 %zext_b)
+ %and = and i16 %clmul, u0x8000
+ %r = icmp eq i16 %and, 0
+ ret i1 %r
+}
+
+define i1 @clmul_high_bits_boundary(i8 %a, i8 %b) nounwind {
+; CHECK-LABEL: @clmul_high_bits_boundary(
+; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i8 [[A:%.*]] to i16
+; CHECK-NEXT: [[ZEXT_B:%.*]] = zext i8 [[B:%.*]] to i16
+; CHECK-NEXT: [[CLMUL:%.*]] = call i16 @llvm.clmul.i16(i16 [[ZEXT_A]], i16 [[ZEXT_B]])
+; CHECK-NEXT: [[R:%.*]] = icmp samesign ult i16 [[CLMUL]], 16384
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %zext_a = zext i8 %a to i16
+ %zext_b = zext i8 %b to i16
+ %clmul = call i16 @llvm.clmul.i16(i16 %zext_a, i16 %zext_b)
+ %and = and i16 %clmul, u0x4000
+ %r = icmp eq i16 %and, 0
+ ret i1 %r
+}
+
+define i1 @clmul_high_bits_negative(i8 %a, i8 %b) nounwind {
+; CHECK-LABEL: @clmul_high_bits_negative(
+; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i8 [[A:%.*]] to i16
+; CHECK-NEXT: [[ZEXT_B:%.*]] = zext i8 [[B:%.*]] to i16
+; CHECK-NEXT: [[CLMUL:%.*]] = call i16 @llvm.clmul.i16(i16 [[ZEXT_A]], i16 [[ZEXT_B]])
+; CHECK-NEXT: [[AND:%.*]] = and i16 [[CLMUL]], 8192
+; CHECK-NEXT: [[R:%.*]] = icmp eq i16 [[AND]], 0
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %zext_a = zext i8 %a to i16
+ %zext_b = zext i8 %b to i16
+ %clmul = call i16 @llvm.clmul.i16(i16 %zext_a, i16 %zext_b)
+ %and = and i16 %clmul, u0x2000
+ %r = icmp eq i16 %and, 0
+ ret i1 %r
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/177893
More information about the llvm-commits
mailing list