[llvm] [AggressiveInstCombine] Make cttz fold more resiliant to non-array geps (PR #150896)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 29 01:21:56 PDT 2025


================
@@ -547,53 +536,53 @@ static bool tryToRecognizeTableBasedCttz(Instruction &I) {
     return false;
 
   GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getPointerOperand());
-  if (!GEP || !GEP->hasNoUnsignedSignedWrap() || GEP->getNumIndices() != 2)
-    return false;
-
-  if (!GEP->getSourceElementType()->isArrayTy())
+  if (!GEP || !GEP->hasNoUnsignedSignedWrap())
     return false;
 
-  uint64_t ArraySize = GEP->getSourceElementType()->getArrayNumElements();
-  if (ArraySize != 32 && ArraySize != 64)
+  Type *GEPSrcEltTy = GEP->getSourceElementType();
+  Value *GepIdx;
+  if (GEP->getNumIndices() == 2) {
+    if (!GEPSrcEltTy->isArrayTy() ||
+        !match(GEP->idx_begin()->get(), m_ZeroInt()))
+      return false;
+    GEPSrcEltTy = GEPSrcEltTy->getArrayElementType();
+    GepIdx = std::next(GEP->idx_begin())->get();
+  } else if (GEP->getNumIndices() == 1)
+    GepIdx = GEP->idx_begin()->get();
+  else
----------------
nikic wrote:

(I'd move the GlobalVariable check below before that though, as it is cheaper.)

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


More information about the llvm-commits mailing list