[llvm] [InstCombine] Make indexed compare fold GEP source type independent (PR #71663)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 21 10:03:41 PST 2023


================
@@ -447,11 +449,11 @@ static bool canRewriteGEPAsOffset(Type *ElemTy, Value *Start, Value *Base,
         return false;
 
       if (auto *GEP = dyn_cast<GEPOperator>(V)) {
-        // We're limiting the GEP to having one index. This will preserve
-        // the original pointer type. We could handle more cases in the
-        // future.
-        if (GEP->getNumIndices() != 1 || !GEP->isInBounds() ||
-            GEP->getSourceElementType() != ElemTy)
+        // Only allow GEPs with at most one variable offset.
+        APInt Offset(IndexSize, 0);
+        MapVector<Value *, APInt> VarOffsets;
+        if (!GEP->collectOffset(DL, IndexSize, VarOffsets, Offset) ||
+            VarOffsets.size() > 1)
----------------
nikic wrote:

I've changed this code to just explicitly count the non-constant indices. There was really no need to use collectOffset() for this purpose here.

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


More information about the llvm-commits mailing list