[llvm] [InstCombine] linearize complexity of findDemandedEltsByAllUsers() (PR #161436)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 30 13:56:49 PDT 2025


================
@@ -320,44 +320,49 @@ Instruction *InstCombinerImpl::foldBitcastExtElt(ExtractElementInst &Ext) {
 }
 
 /// Find elements of V demanded by UserInstr.
-static APInt findDemandedEltsBySingleUser(Value *V, Instruction *UserInstr) {
-  unsigned VWidth = cast<FixedVectorType>(V->getType())->getNumElements();
+static void findDemandedEltsBySingleUser(Value *V, Instruction *UserInstr,
+                                         APInt &UnionUsedElts) {
+  const unsigned VWidth = cast<FixedVectorType>(V->getType())->getNumElements();
 
-  // Conservatively assume that all elements are needed.
-  APInt UsedElts(APInt::getAllOnes(VWidth));
+  // Whether we can determine the elements accessed at compile time.
+  bool KnownIndices = false;
 
   switch (UserInstr->getOpcode()) {
   case Instruction::ExtractElement: {
     ExtractElementInst *EEI = cast<ExtractElementInst>(UserInstr);
     assert(EEI->getVectorOperand() == V);
     ConstantInt *EEIIndexC = dyn_cast<ConstantInt>(EEI->getIndexOperand());
     if (EEIIndexC && EEIIndexC->getValue().ult(VWidth)) {
-      UsedElts = APInt::getOneBitSet(VWidth, EEIIndexC->getZExtValue());
+      UnionUsedElts.setBit(EEIIndexC->getZExtValue());
+      KnownIndices = true;
----------------
nikic wrote:

Instead of having a boolean flag, can we early return in cases where the indices are known instead?

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


More information about the llvm-commits mailing list