[llvm] [IR][InstSimplify] Clarify poison elements in cttz.elts results in poison (PR #223935)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 16 00:34:03 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Luke Lau (lukel97)

<details>
<summary>Changes</summary>

The generic expansion for cttz.elts in LegalizeVectorOps is

    (reduce.umax (select input, step_vector, 0))

Because reductions produce poison if any element is poison, this will also produce poison.

ConstantFolding currently has short circuiting behaviour, e.g. `cttz.elts <0, 1, poison, poison> -> 1`, but we should probably make this consistent with reductions and just return poison. This updates LangRef and ConstantFolding to be more explicit about this.


---
Full diff: https://github.com/llvm/llvm-project/pull/223935.diff


3 Files Affected:

- (modified) llvm/docs/LangRef.md (+1) 
- (modified) llvm/lib/Analysis/ConstantFolding.cpp (+2-1) 
- (modified) llvm/test/Transforms/InstSimplify/ConstProp/cttz-elts.ll (+2-2) 


``````````diff
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index a29e8f7508a7f..c59132e9b1da5 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -21170,6 +21170,7 @@ The '`llvm.experimental.cttz.elts`' intrinsic counts the trailing (least
 significant) zero elements in a vector. If `src == 0` the result is the
 number of elements in the input vector.
 
+If any element in the input vector is poison, the result is poison.
 
 #### '`llvm.experimental.get.vector.length`' Intrinsic
 
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index aca2556cd48eb..05b7c0c52a5d1 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3969,7 +3969,8 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
     if (!FVTy)
       return nullptr;
     unsigned Width = Ty->getIntegerBitWidth();
-    if (APInt::getMaxValue(Width).ult(FVTy->getNumElements()))
+    if (APInt::getMaxValue(Width).ult(FVTy->getNumElements()) ||
+        Operands[0]->containsPoisonElement())
       return PoisonValue::get(Ty);
     for (unsigned I = 0; I < FVTy->getNumElements(); ++I) {
       Constant *Elt = Operands[0]->getAggregateElement(I);
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/cttz-elts.ll b/llvm/test/Transforms/InstSimplify/ConstProp/cttz-elts.ll
index db91fd68fcbe6..928cb3ddde2a9 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/cttz-elts.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/cttz-elts.ll
@@ -43,7 +43,7 @@ define i32 @cttz_elts_v4i32_used_lane_undef() {
 
 define i32 @cttz_elts_v4i32_unused_lane_poison() {
 ; CHECK-LABEL: @cttz_elts_v4i32_unused_lane_poison(
-; CHECK-NEXT:    ret i32 1
+; CHECK-NEXT:    ret i32 poison
 ;
   %res = call i32 @llvm.experimental.cttz.elts.i32.v4i32(<4 x i32> <i32 0, i32 1, i32 poison, i32 3>, i1 false)
   ret i32 %res
@@ -51,7 +51,7 @@ define i32 @cttz_elts_v4i32_unused_lane_poison() {
 
 define i32 @cttz_elts_v4i32_used_lane_poison() {
 ; CHECK-LABEL: @cttz_elts_v4i32_used_lane_poison(
-; CHECK-NEXT:    ret i32 3
+; CHECK-NEXT:    ret i32 poison
 ;
   %res = call i32 @llvm.experimental.cttz.elts.i32.v4i32(<4 x i32> <i32 0, i32 0, i32 poison, i32 3>, i1 false)
   ret i32 %res

``````````

</details>


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


More information about the llvm-commits mailing list