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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 13 14:07:37 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:

VarOffsets.empty() means that the GEP has a constant offset, not that it doesn't have indices.

(As a side-note, zero-index GEPs are indeed allowed, but they are always no-ops and optimized away.)

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


More information about the llvm-commits mailing list