[llvm] [NFC][IR] Add CreateCountTrailingZeroElems helper (PR #106711)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 30 04:05:32 PDT 2024


https://github.com/david-arm created https://github.com/llvm/llvm-project/pull/106711

The LoopIdiomVectorize pass already creates calls to the intrinsic
experimental_cttz_elts, but PR #88385 will start calling this more
too so I've created a helper for it.

>From 092349da1cebf1ebf8ffecc6dbe5b190b45dd0f5 Mon Sep 17 00:00:00 2001
From: David Sherwood <david.sherwood at arm.com>
Date: Fri, 30 Aug 2024 11:01:22 +0000
Subject: [PATCH] [NFC][IR] Add CreateCountTrailingZeroElems helper

The LoopIdiomVectorize pass already creates calls to the intrinsic
experimental_cttz_elts, but PR #88385 will start calling this more
too so I've created a helper for it.
---
 llvm/include/llvm/IR/IRBuilder.h                     | 8 ++++++++
 llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp | 5 ++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 0dbcbc0b2cb76f..d85f51a0a01210 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -1079,6 +1079,14 @@ class IRBuilderBase {
                            nullptr, Name);
   }
 
+  /// Create a call to llvm.experimental_cttz_elts
+  Value *CreateCountTrailingZeroElems(Type *ResTy, Value *Mask,
+                                      const Twine &Name = "") {
+    return CreateIntrinsic(
+        Intrinsic::experimental_cttz_elts, {ResTy, Mask->getType()},
+        {Mask, getInt1(/*ZeroIsPoison=*/true)}, nullptr, Name);
+  }
+
 private:
   /// Create a call to a masked intrinsic with given Id.
   CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
diff --git a/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
index cb31e2a2ecaec4..45c0eae29c9e9a 100644
--- a/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopIdiomVectorize.cpp
@@ -470,9 +470,8 @@ Value *LoopIdiomVectorize::createMaskedFindMismatch(
   VectorFoundIndex->addIncoming(VectorIndexPhi, VectorLoopStartBlock);
 
   Value *PredMatchCmp = Builder.CreateAnd(LastLoopPred, FoundPred);
-  Value *Ctz = Builder.CreateIntrinsic(
-      Intrinsic::experimental_cttz_elts, {ResType, PredMatchCmp->getType()},
-      {PredMatchCmp, /*ZeroIsPoison=*/Builder.getInt1(true)});
+  Value *Ctz =
+      Builder.CreateCountTrailingZeroElems(ResType, PredMatchCmp);
   Ctz = Builder.CreateZExt(Ctz, I64Type);
   Value *VectorLoopRes64 = Builder.CreateAdd(VectorFoundIndex, Ctz, "",
                                              /*HasNUW=*/true, /*HasNSW=*/true);



More information about the llvm-commits mailing list