[llvm] [CVP] Remove Zero-Index GEP (PR #144831)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 18 23:12:02 PDT 2025


================
@@ -1252,6 +1253,23 @@ static bool processTrunc(TruncInst *TI, LazyValueInfo *LVI) {
   return Changed;
 }
 
+// remove a GEP if all indices are in range [0, 1)
+static bool processGEP(GetElementPtrInst *GEP, LazyValueInfo *LVI) {
+  for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) {
+    ConstantRange Range = LVI->getConstantRangeAtUse(GEP->getOperandUse(i),
+                                                     /*UndefAllowed=*/false);
+    const APInt *C = Range.getSingleElement();
+    if (!C || !C->isZero())
+      return false;
+  }
+
+  // all indices are zero
+  Value *Ptr = GEP->getPointerOperand();
+  GEP->replaceAllUsesWith(Ptr);
----------------
dtcxzyw wrote:

It doesn't work for the form `getelementptr ty, scalar ptr, vector indices`. You can create a splat when the type of GEP differs from the type of source pointer.

Crash reproducer:
```
define <2 x ptr> @noop_gep_nonzero_index_vec(ptr %ptr, <2 x i64> range(i64 0, 1) %index) {
  %gep = getelementptr i8, ptr %ptr, <2 x i64> %index
  ret <2 x ptr> %gep
}
```

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


More information about the llvm-commits mailing list